我的“ View ”设置如下。简单。
var ItemView = Backbone.View.extend({
tagName : "li",
events : {
"click" : "display"
},
display : function() {
//app.navigate('item'); // take me to my route!
}
});
我有我的路由器
var App = Backbone.Router.extend({
routes: {
"" : "index",
"item" : "view_item"
},
index: function() {
alert('hi!');
},
view_item: function() {
alert('bye!');
}
});
app = new App();
Backbone.history.start();
现在,当我点击 ItemView 时,它应该运行“显示”方法,我希望显示方法将我带到我在路由“项目”中指定的路由。
这可能吗?我以为“导航”功能会起作用,但事实并非如此。我怎样才能做到这一点?
最佳答案
display : function() {
app.navigate('item', true);
}
您需要将第二个参数设置为 true。
来自 Backbone 文档:
navigaterouter.navigate(fragment, [triggerRoute]) Whenever you reach a point in your application that you'd like to save as a URL, call navigate in order to update the URL. If you wish to also call the route function, pass triggerRoute.
关于javascript - Backbone.js - 在 View 上的 "click"事件之后导航到路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7511567/