最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbone.history.navigate(url,{trigger:true, replace: true}) - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question edited Apr 17, 2017 at 10:22 Donald Duck 8,89223 gold badges79 silver badges102 bronze badges asked Dec 23, 2013 at 8:42 user2508256user2508256 1031 gold badge1 silver badge4 bronze badges 2
  • 2 Is the problem the # in the url? The question is not very clear. – fearphage Commented Jan 13, 2014 at 23:58
  • 1 It seems the answer below should be accepted as the correct answer, please note you seemed to have misinterpreted the meaning of replace---it replaces the entry in the history (the address bar always changes on navigation)... but navigation is not the only way to update your app! – Corin Commented Feb 16, 2015 at 2:01
Add a comment  | 

1 Answer 1

Reset to default 15

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.

发布评论

评论列表(0)

  1. 暂无评论