Trying to understand the insides of backbone.js (Todos App) framework by adding some extra functionalities, my Todos App triggers an collection.add event on both collections, the original TodoList and my own UnsavedList
This is what I did:
Adding an 'Mark All Done' button to document trough _.template that checks all unchecked checkboxes without saving changes to the server.
Adding an 'Save ({unsaved collection length})' button to document trough _.template.
To accomplish this, I keep track of the 'savedState' of all models in TodoList
// inside TodoList
hasUnsaved: function() {
return this.filter(function(todo) {
var isEqual = _.isEqual(todo.savedAttributes, todo.attributes);
return !isEqual;
})
},
Declaring and instantiating UnsavedList:
// inside window
window.UnsavedList = Backbone.Collection.extend({
model: Todo,
initialize: function() {
this.bind('add', this.addHandler)
},
addHandler: function() {
alert('added')
}
});
window.UnsavedTodos = new UnsavedList;
As in the original sample, the add event is bound to addOne Handler.
// inside AppView
...
initialize: function() {
Todos.bind('add', this.addOne);
...
}
Listens to 'Mark All Done' Button click to invoke markAllDone which in turn triggers 'change:allDone':
// inside AppView
markAllDone: function() {
Todos.each(function(todo) {
todo.set({ done: true });
})
Todos.trigger('change:allDone');
},
... and 'change:allDone' invokes:
// inside AppView
checkSaved: function() {
this.$('#todo-controls #button-saved').html(this.buttonSavedTemplate({
saved: function() {
var list = Todos.hasUnsaved();
_.each(list, function(todo) {
UnsavedTodos.add(todo);
})
return list.length;
}
}));
},
If you look at the iterator of my template object, I'm adding all unsaved models to my UnsavedList. But strangely enough TodoList also listens to this exact 'add' event at what all unsaved models show up duplicated on the page.
Why is that, even tough I 'added' to UnsavedList only.
Thoughts ?