Backbone.js Tutorial - 13 - (Collections) Modifying Elements

C:\Users\Administrator\Desktop\gulp\app.js

StudentModel = Backbone.Model.extend({
	defaults: {
		name: "unknown"
	}
});
StudentCollection = Backbone.Collection.extend({
	model: StudentModel,
	initialize: function () {

	}
});
jQuery(document).ready(function($) {
	var jony = new StudentModel({name: "Lionel1",id: 1});
	var vicky = new StudentModel({name: "Lionel2",id: 2});
	var studentGroup = new StudentCollection([jony, vicky]);
	displayCollectionContents("Before: ", studentGroup);
	var student = studentGroup.get(1);
	student.set({name: "Edit 1"});
	displayCollectionContents("After: ", studentGroup);
});
function displayCollectionContents(string, collection) {
	console.log(string + " " + JSON.stringify(collection.toJSON()));
}

C:\Users\Administrator\Desktop\gulp\app.js

StudentModel = Backbone.Model.extend({
	defaults: {
		name: "unknown"
	}
});
StudentCollection = Backbone.Collection.extend({
	model: StudentModel,
	initialize: function () {

	}
});
jQuery(document).ready(function($) {
	var jony = new StudentModel({name: "Lionel1",id: 1});
	var vicky = new StudentModel({name: "Lionel2",id: 2});
	var studentGroup = new StudentCollection([jony, vicky]);
	displayCollectionContents("Before: ", studentGroup);
	var student = studentGroup.get(1);
	student.set({name: "Edit 1"});
	displayCollectionContents("After 1: ", studentGroup);
	vicky.set({name: "Edit 3"});
	displayCollectionContents("After 2: ", studentGroup);
});
function displayCollectionContents(string, collection) {
	console.log(string + " " + JSON.stringify(collection.toJSON()));
}

Last updated

Navigation

Lionel

@Copyright 2023