StudentModel = Backbone.Model.extend({
defaults: {
name: "unknown"
}
});
StudentCollection = Backbone.Collection.extend({
model: StudentModel,
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
this.on("change:name", function(model) {
console.log("Change name: " + model.get('name'));
}),
this.on("add", function(model) {
console.log("Add studient: " + model.get('name'));
}),
this.on("remove", function(model) {
console.log("Remove studient: " + model.get('name'));
})
}
});
jQuery(document).ready(function($) {
var jony = new StudentModel({ name: "Lionel1", id: 1 });
var vicky = new StudentModel({ name: "Lionel2", id: 2 });
var studentGroup = new StudentCollection([vicky]);
studentGroup.add([jony]);
studentGroup.remove([jony]);
vicky.set({ name: "Edit 1" });
displayCollectionContents("After: ", studentGroup);
});
function displayCollectionContents(string, collection) {
console.log(string + " " + JSON.stringify(collection.toJSON()));
}