I am trying to detect created event in component. Nothing happens.
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");
}
}
});
The model is
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();
}
}, {});
Markup is
<input type="text" name="todo" placeholder="What needs to be done" can-enter="todoCreated" />
Is this correct way of handling created event or is there a better way?
I need list to react when new todo is created.
See here for code