I want to call collection’s method from the AppView. But It can’t. My code is like this.
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;
});
app.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")});
});
But I don’t know why this.collection.update() method outputs undefined. Do you have any idea?
And my all code is here https://github.com/haradashinya/my-todo-redis
Thanks in advance.
The mistake is in your
app.jsdefinition. It should returnAppViewnot an instance of it, the instance should be created outside the module definition. As it is now, by the time it returnstodolistis not yet defined.