diff --git a/assets/javascripts/app/router.coffee b/assets/javascripts/app/router.coffee index 91b2cfef..3b167e1b 100644 --- a/assets/javascripts/app/router.coffee +++ b/assets/javascripts/app/router.coffee @@ -34,19 +34,24 @@ class app.Router return before: (context, next) -> + previousContext = @context @context = context @trigger 'before', context - next() - return + + if res = next() + @context = previousContext + return res + else + return doc: (context, next) -> if doc = app.docs.findBySlug(context.params.doc) or app.disabledDocs.findBySlug(context.params.doc) context.doc = doc context.entry = doc.toEntry() @triggerRoute 'entry' + return else - next() - return + return next() type: (context, next) -> doc = app.docs.findBySlug(context.params.doc) @@ -55,9 +60,9 @@ class app.Router context.doc = doc context.type = type @triggerRoute 'type' + return else - next() - return + return next() entry: (context, next) -> doc = app.docs.findBySlug(context.params.doc) @@ -66,36 +71,39 @@ class app.Router context.doc = doc context.entry = entry @triggerRoute 'entry' + return else - next() - return + return next() root: -> - if app.isSingleDoc() - setTimeout (-> window.location = '/'), 0 - else - @triggerRoute 'root' + return '/' if app.isSingleDoc() + @triggerRoute 'root' return - settings: -> + settings: (context) -> + return "/#/#{context.path}" if app.isSingleDoc() @triggerRoute 'settings' return - offline: -> + offline: (context)-> + return "/#/#{context.path}" if app.isSingleDoc() @triggerRoute 'offline' return about: (context) -> + return "/#/#{context.path}" if app.isSingleDoc() context.page = 'about' @triggerRoute 'page' return news: (context) -> + return "/#/#{context.path}" if app.isSingleDoc() context.page = 'news' @triggerRoute 'page' return help: (context) -> + return "/#/#{context.path}" if app.isSingleDoc() context.page = 'help' @triggerRoute 'page' return diff --git a/assets/javascripts/lib/page.coffee b/assets/javascripts/lib/page.coffee index bfe454a0..3f40be38 100644 --- a/assets/javascripts/lib/page.coffee +++ b/assets/javascripts/lib/page.coffee @@ -38,11 +38,15 @@ page.stop = -> page.show = (path, state) -> return if path is currentState?.path context = new Context(path, state) + previousState = currentState currentState = context.state - page.dispatch(context) - context.pushState() - updateCanonicalLink() - track() + if res = page.dispatch(context) + currentState = previousState + location.assign(res) + else + context.pushState() + updateCanonicalLink() + track() context page.replace = (path, state, skipDispatch, init) -> @@ -58,10 +62,9 @@ page.replace = (path, state, skipDispatch, init) -> page.dispatch = (context) -> i = 0 next = -> - fn(context, next) if fn = callbacks[i++] - return - next() - return + res = fn(context, next) if fn = callbacks[i++] + return res + return next() page.canGoBack = -> not Context.isIntialState(currentState) @@ -116,10 +119,9 @@ class Route (context, next) => if @match context.pathname, params = [] context.params = params - fn(context, next) + return fn(context, next) else - next() - return + return next() match: (path, params) -> return unless matchData = @regexp.exec(path) diff --git a/assets/javascripts/views/content/offline_page.coffee b/assets/javascripts/views/content/offline_page.coffee index 97cae8d4..58f2a181 100644 --- a/assets/javascripts/views/content/offline_page.coffee +++ b/assets/javascripts/views/content/offline_page.coffee @@ -45,11 +45,8 @@ class app.views.OfflinePage extends app.View docEl: (doc) -> @find("[data-slug='#{doc.slug}']") - onRoute: (route) -> - if app.isSingleDoc() - window.location = "/#/#{route.path}" - else - @render() + onRoute: (context) -> + @render() return onClick: (event) => diff --git a/assets/javascripts/views/content/settings_page.coffee b/assets/javascripts/views/content/settings_page.coffee index c3671c29..d1d3d0db 100644 --- a/assets/javascripts/views/content/settings_page.coffee +++ b/assets/javascripts/views/content/settings_page.coffee @@ -58,9 +58,6 @@ class app.views.SettingsPage extends app.View @toggle input.name, input.checked return - onRoute: (route) => - if app.isSingleDoc() - window.location = "/#/#{route.path}" - else - @render() + onRoute: (context) -> + @render() return