Backbone js subRouter

时间:2012-02-12 03:51:22

标签: javascript backbone.js

如何为我的骨干应用程序创建子路由器? 我找到了一些代码,但无法使其正常工作(https://gist.github.com/1235317)。路由器创建后不会执行路由处理程序。

1 个答案:

答案 0 :(得分:1)

将其添加到最后的构造函数

// grab the full URL
var hash = Backbone.history.getHash();

// check if there is already a part of the URL that this subview cares about...
var hashPart = hash.substr(prefix.length + 1, hash.length);

// ...if so, trigger the subroute immediately.  this supports the case where 
// a user directly navigates to a URL with a subroute on the first page load.
if (hashPart && hashPart != "") {
    Backbone.history.loadUrl(prefix + hashPart);
}