0
我對Rails TodoList應用程序有以下看法。沒有javascript錯誤。我甚至可以通過.fetch()方法看到/ todos的GET請求。問題是,沒有調用addOne函數,因此沒有視圖更新。爲什麼Collection.fetch()不更新視圖?
$(function(){
Todos = new TodoList.Collections.Todos;
TodoList.Views.TodoView = Backbone.View.extend({
tagName: "li",
events: {},
initialize: function(){},
template: _.template('<li> {{task }}</li>'),
render: function(){
var todo = this.model.toJSON();
//return this.template(todo);
return "blah";
}
});
TodoView = TodoList.Views.TodoView;
TodoList.Views.AppView = Backbone.View.extend({
el: $("#todo_app"),
events: {
"submit form#new_todo": "createTodo"
},
initialize: function(){
_.bindAll(this, 'addOne', 'addAll','render');
Todos.bind("add", this.addOne);
Todos.bind("refresh", this.addAll);
Todos.bind("all", this.render);
Todos.fetch();
},
addOne: function(todo){
var view = new TodoView({model: todo});
this.$("#todo_list").append(view.render().el);
alert("here");
},
addAll: function(){
Todos.each(this.addone);
},
newAttributes: function(event){
var new_todo_form = $(event.currentTarget).serializeObject();
return {
dog: {
name: new_todo_form["todo[task]"],
age: new_todo_form["todo[done]"]
}
};
},
createTodo: function (e){
e.preventDefault();
var params = this.newAttributes(e);
Dogs.create(params);
}
});
});
而且,我試圖調用它使用
this.addOne({task: "blah", done:"true"});
在初始化函數手動。這會拋出javascript錯誤,說JSON函數是未定義的。
我相信事件已更名爲'reset'。所以試試這個'Todos.bind(「reset」,this.addAll);' – Deeptechtons
啊..救了我的一天。禮炮。 –