Backbone.history.navigate(url,{trigger:true, replace: true})

user2508256 picture user2508256 · Dec 23, 2013 · Viewed 22.4k times · Source

I used this method to navigate to url, trigger an event and not push url to browser history. But Backbone.history.navigate(url,{trigger:true, replace: true}) replace previous url from history. Example: Browser hystory

Before

localhost:port/url

Backbone.history.navigate(url + '/list',{ trigger:true, replace: true })

Expected: trigger event on route url + '/list' and Browser history localhost:port/url Actually: triggering event but Browser history localhost:port/#url/list. Previous url is replaced

Answer

Aubricus picture Aubricus · Feb 10, 2014

Here's what I think you're asking, please correct me if I'm wrong:

"How do I trigger a route in Backbone without updating the URL?"

Quick Answer:

You can't. The replace option will also not help you there. Here's a snippet from the docs:

To update the URL without creating an entry in the browser's history, set the replace option to true.

As you can see replace does not affect the update to the URL.

Subsequent Answer:

If you don't want the URL to update, this makes me think that showing a list in your application is not a major update to your application state. If that's true, then you shouldn't, in my experience, use the Backbone.history object to manage it.

If you called this functionality either directly (e.g., through a function) or indirectly (e.g., by dispatching an event) then your app would update and your URL wouldn't.

I like Derick Baily's Post on how to set up your router structure. It's a bit lengthy but worth a read.

Also, check out David Sulc's repo for "A Gentle Introduction to Marionette". I know it's using Marionette.AppRouter objects, but the principals are still sound. Check out this file for an example.

As a side note, if the # is the problem; check out this SO answer. Be wary, however, the hashChange option is mostly for x-browser compatibility according to the docs.