0
我正試圖檢測組件中創建的事件。什麼都沒發生。Canjs創建事件
can.Component.extend({
// todos-list component
// lists todos
tag: "todos-list",
template: can.view("javascript_view/todos-list"),
scope: {
Todo: Todo,
todos: TodoList.slice(0),
todoCreated: function(context, element) {
// new todo is created
var Todo = this.Todo;
new Todo({
name: can.trim(element.val())
}).save();
element.val("");
}
},
events: {
"{Todo} created": function(Todo, event, newTodo) {
console.log("created");
}
}
});
該模型是
var Todo = can.Model.extend({
// todo model
// fetches things and so on
findAll: function() {
// get all todos
return $.Deferred().resolve(todos);
},
findOne: function(params) {
// get one todo
return $.Deferred().resolve(todos[(+params.id) - 1]);
},
create: function(attributes) {
// creates new todo
var last = todos[todos.length - 1];
$.extend(attributes, {id: last.id + 1, detail: "", tag: ""});
todos.push(attributes);
return $.Deferred().resolve(attributes);
},
update: function(id, attributes) {
// update one todo
$.extend(todos[id - 1], attributes);
return $.Deferred().resolve();
},
destroy: function() {
// destroy todo
return $.Deferred().resolve();
}
}, {});
標記是
<input type="text" name="todo" placeholder="What needs to be done" can-enter="todoCreated" />
是處理創建的事件的這種正確的做法還是有更好的辦法?
我需要列表來反應,當新的待辦事項創建。
參見here for code
'todos'數組來自哪裏? – 2014-10-07 03:16:24
你渲染一個包含''元素的模板嗎?你是否在頁面中插入了該模板的結果? –
2014-10-07 03:17:59
todos有一個var TodoList = new Todo.List({})的實例,是的,我正在渲染包含待辦事項列表的模板 – 2014-10-07 11:21:51