Using Backbone.history to return without running a route function

I have a modal popup that changes the URL when opened. When the user closes the popup, I want to return to the previous URL, but I do not want to start the route associated with this URL, because it will reload my collection and display the view, etc. Is there a way to call window.history.back() without starting a route, or is there an equivalent to this base unit?

The only solution I can think of is to keep the previous route, and then when the modal call is closed

 Backbone.history.navigate(route, {trigger: false, replace: true}); 

but this seems like a difficult way to solve a simple problem.

+6
source share
1 answer

Storing history in the router sounds like a good solution for me, I could not find a better way to solve this problem.

Good solution here: Silently change the URL to the previous page using Backbone.js

I would make a little tweak so that it looks like this:

 class MyRouter extends Backbone.Router initialize: (options) -> @on "all", @storeRoute @history = [] storeRoute: -> @history.push Backbone.history.fragment previous: -> if @history.length > 1 @navigate @history[@history.length-2], false else @navigate '', true 

Then you can simply call MyRouter.previous (), and if you came by direct request, it will lead you to your root.

I would like this to be the default function for the router, at least so that it saves the last 5 routes.

+5
source

Source: https://habr.com/ru/post/919863/


All Articles