我有一个Rails 3.2.3应用与Backbone.js和我在我的Backbone.history上使用pushState。Backbone.js路由没有被触发
的问题
当我点击它去说“/富”,以显示与ID任命一个链接:1,然后骨干路由器到达,首先,我可以前滑轨路由器快速查看接管并抱怨没有/ foo路线。
我Backbone.js的代码
这是我的骨干路由器。
window.AppointmentApp = new (Backbone.Router.extend({
routes: {
"": "index",
"foo": "foo",
"appointments": "index",
"appointments/:id": "show"
},
foo: function(){
$("#app").append("foo<br />");
},
initialize: function() {
this.appointments = new Appointments();
this.appointmentListView = new AppointmentListView({ collection: this.appointments });
this.appointmentListView.render();
},
start: function() {
Backbone.history.start({pushState: true});
},
index: function() {
$("#app").html(this.appointmentListView.el);
this.appointments.fetch();
},
show: function(id) {
console.log("Enter show");
}
}));
它应该停留在同一页面上并在#app div附加一个'foo',但它永远不会。
骨干指数观众
window.AppointmentListView = Backbone.View.extend({
template: JST["appointments/index"],
events: {
"click .foo": function(){Backbone.history.navigate("foo");},
},
comparator: function(appointment){
return appointment.get('topic');
},
initialize: function(){
this.collection.on('reset', this.addAll, this);
},
render: function(){
this.$el.html(this.template);
this.addAll();
return this;
},
addAll: function() {
this.collection.forEach(this.addOne, this);
},
addOne: function(appointment){
var appointmentView = new AppointmentView({model: appointment});
this.$el.append(appointmentView.render().el);
}
});
应用程序/资产/模板/约会/ Index.jst.ejs
<h1>Appointments</h1>
<a href="/foo" class="foo">Say Foo</a>
<a href=appointments/add>Add</a>
<div id="app"></div>
我用pushState的,因为它让我保持历史和后退按钮功能。
Backbone.history.navigate不会调用我的Backbone路由,而是调用Rails路由。我该如何解决这个问题?
我应该试图设置Backbone来接受诸如'约会/ 1'之类的路线并采取控制措施,还是必须使用像上面那样的Backbone.history.navigate调用来使用单击事件?
我认为pushState背后的想法是你不必使用URL片段? – map7 2012-04-11 23:50:57
如果我删除pushState并将您的导航行更改为“#/ foo”,那么它一切正常,但URL片段不是真正的URL。我正在寻找一种方法来实现pushState – map7 2012-04-12 00:03:17
@ map7啊,对不起,我不太明白你在问什么。我已经更新了我的答案 – obmarg 2012-04-12 09:25:51