Remember history to support browser back with @navigate used in redirects.

This commit is contained in:
Martin Edenhofer 2015-09-02 20:05:48 +02:00
parent f9d027d180
commit 37f1a765e4
4 changed files with 19 additions and 9 deletions

View file

@ -29,14 +29,16 @@ class App.Controller extends Spine.Controller
ajaxId = App.Ajax.request(data)
@ajaxCalls.push ajaxId
navigate: (location, hide_current_location_from_history = false) ->
@log 'notice', "navigate to '#{location}'"
navigate: (location, hideCurrentLocationFromHistory = false) ->
@log 'notice', "navigate to '#{location}', hide from history '#{hideCurrentLocationFromHistory}'"
# hide current location from browser history, allow to use back button in browser
if hide_current_location_from_history
@log 'debug', "ignore new location from browser histroy '#{location}'"
if hideCurrentLocationFromHistory
if window.history
window.history.replaceState(null, null, location)
history = App.Config.get('History')
oldLocation = history[history.length-2]
if oldLocation
window.history.replaceState(null, null, oldLocation)
super location
bind: (event, callback) =>

View file

@ -1,4 +1,5 @@
App.Config.set( 'User', {
App.Config.set('History', [])
App.Config.set('User', {
prio: 1000,
parent: '',
callback: ->

View file

@ -1,4 +1,4 @@
class Index extends App.Controller
class DefaultRouter extends App.Controller
constructor: ->
super
@ -28,5 +28,5 @@ class Index extends App.Controller
@navigate '#dashboard', true
App.Config.set( '', Index, 'Routes' )
App.Config.set( '/', Index, 'Routes' )
App.Config.set( '', DefaultRouter, 'Routes' )
App.Config.set( '/', DefaultRouter, 'Routes' )

View file

@ -87,6 +87,13 @@ class App.Content extends App.ControllerWidgetPermanent
params: params_only,
)
# remember history
# needed to mute "redirect" url to support browser back
history = App.Config.get('History')
if history[10]
history.pop()
history.push window.location.hash
# execute controller
controller = (params) =>
params.el = @el