我正在尝试检测组件中创建的事件。什么都没发生。
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" />
这是处理创建事件的正确方法还是有更好的方法?
创建新的待办事项时,我需要列表做出反应。
代码见这里