Silently change url to previous using Backbone.js

帅比萌擦擦* 提交于 2019-12-09 05:32:50

问题


Using Backbone.js, is it possible to make the router navigate to the page where it came from? I'd like to use that for the case where I change my URL when a popup appears, and I want go change it back, when I hide the popup. I don't want to simply go back, because I want to keep the background page in precisely the same position as I left it before I showed the popup


回答1:


You can solve this problem by extending Backbone.Router and storing all the routes during navigation.

class MyRouter extends Backbone.Router
  constructor: (options) ->
    @on "all", @storeRoute
    @history = []
    super options

  storeRoute: ->
    @history.push Backbone.history.fragment

  previous: ->
    if @history.length > 1
      @navigate @history[@history.length-2], true 

Then, when you have to dismiss your modal, simply call the MyRouter.previous() method that it will redirect you back to the last "hash".




回答2:


I think mateusmaso's answer is mostly right but requires some tweaks to guarentee that you always get the right URL you are looking for.

First you need to override the route method to have a beforeRoute method fire:

route: (route, name, callback) =>
   Backbone.Router.prototype.route.call(this, route, name, =>
     @trigger('beforeRoute')
     callback.apply(this, arguments)
   )

Then you bind the event and initialize the history instance variable:

initialize: (options) ->       
   @history = []
   @on "beforeRoute", @storeRoute

Next create helper methods for storing and retrieving the fragment:

storeRoute: =>
   @history.push Backbone.history.fragment

previousFragment: =>
   @history[@history.length-2]

Finally, you need one final helper method that can be used to change the URL without reloading and store the resulting fragment. You need to use this when closing the pop up or you won't have the expected fragment in your history if the user gets the pop up again without navigating anywhere else. This is because calling navigate without "trigger: true" won't trigger the event handler to store the fragment.

changeAndStoreFragment: (fragment) =>
   @navigate(fragment)
   @storeRoute()



回答3:


This answer may not address the question, but it's preety much the same issue. I couldn't navigate silently to a previous route, or a custom route because of the trailing slash. In order for route functions to NOT be triggered, use {trigger:false}, or don't use trigger at all since false is the default behaviour, and make sure your route begins with #something instead of '#/something' (notice the slash), or change the regEx inside Backbone.js, the router part.




回答4:


You can trigger a route in the onCloseEvent of your popup or overlay with:

router.navigate('/lasturl/');

This will set the url. If you pass true as the second param, you also will execute the routes action. Otherwise the page will be left unchanged.

http://documentcloud.github.com/backbone/#Router-navigate



来源:https://stackoverflow.com/questions/8416074/silently-change-url-to-previous-using-backbone-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!