我想从 AppView 调用集合的方法。但它不能。我的代码是这样的。
todolist.js
define(["todolist","todo"],function(TodoList,Todo){
var TodoList = Backbone.Collection.extend({
url: "/todos",
initialize:function(){
this.fetch();
this.bind("add",this.update,this);
},
add:function(){
console.log("added");
},
update:function(){
console.log("update!");
}
});
return TodoList;
});
应用程序.js
define(["jquery","underscore","backbone","todo","todolist","todo_view"],
function($,_,Backbone,Todo,TodoList,TodoView){
var AppView = Backbone.View.extend({
initialize:function(){
this.collection = new TodoList;
this.collection.update();
},
events:{
"keypress #new-todo":"create",
},
create:function(e){
if (e.keyCode === 13){
var txt = $("#new-todo").val();
var todo = new Todo({title: txt});
this.collection.add(todo.toJSON());
}
}
}
);
return new AppView({el: $("body")});
});
但我不知道为什么 this.collection.update() 方法输出未定义。你有什么主意吗?我的所有代码都在这里https://github.com/haradashinya/my-todo-redis
提前致谢。