What's the difference between Backbone.history.navigate and this.router.navigate

我们两清 提交于 2019-12-08 03:08:12

问题


What is the difference between Backbone.history.navigate and this.router.navigate?

Why would sometimes the former work while later won't?


回答1:


If you look at the Backbone source, you'll see that Router.prototype.navigate is simply a proxy to Backbone.history.navigate which you can call anywhere without the need for a router instance.

// Simple proxy to `Backbone.history` to save a fragment into the history.
navigate: function(fragment, options) {
  Backbone.history.navigate(fragment, options);
  return this;
},

Routing is handled in a global, namespaced in Backbone, History instance.

This is meant to let developer create their own History class and then to overwrite the Backbone.history property to globally change the routing behaviour.

The History class isn't documented much, but it's well commented in the source.

Also, having a proxy to navigate in the Router class makes it easy to hook our own behaviour in the router directly.


As to why sometimes it doesn't work is probably because you're trying to do this.router.navigate in a class where this.router doesn't exist.



来源:https://stackoverflow.com/questions/43883816/whats-the-difference-between-backbone-history-navigate-and-this-router-navigate

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