The document defines a Comment model and CommentCollection in Backbone.js. The Comment model stores individual comments with attributes like name, email, text, and date. It uses localStorage to save models. The CommentCollection manages multiple Comment models and also uses localStorage. New comments can be added to the collection after saving individual models. Models trigger events like 'change' when data changes.
1 of 207
Downloaded 67 times
More Related Content
JSDay Italy - Backbone.js
33. var Animal, myAnimal;
Animal = function (name) {
this.name = name || 'Unknown';
}
Animal.prototype = {
say: function () {
console.log(
'Hi, my name is ' + this.name + '!'
);
}
}
myAnimal = new Animal('Bello');
myAnimal.say(); // Hi, my name is Bello!
34. var Animal, myAnimal;
Animal = function (name) {
this.name = name || 'Unknown';
}
Animal.prototype = {
say: function () {
console.log(
'Hi, my name is ' + this.name + '!'
);
}
}
myAnimal = new Animal('Bello');
myAnimal.say(); // Hi, my name is Bello!
35. var Animal, myAnimal;
Animal = function (name) {
this.name = name || 'Unknown';
}
Animal.prototype = {
say: function () {
console.log(
'Hi, my name is ' + this.name + '!'
);
}
}
myAnimal = new Animal('Bello');
myAnimal.say(); // Hi, my name is Bello!
36. var Animal, myAnimal;
Animal = function (name) {
this.name = name || 'Unknown';
}
Animal.prototype = {
say: function () {
console.log(
'Hi, my name is ' + this.name + '!'
);
}
}
myAnimal = new Animal('Bello');
myAnimal.say(); // Hi, my name is Bello!
37. var Animal, myAnimal;
Animal = function (name) {
this.name = name || 'Unknown';
}
Animal.prototype = {
say: function () {
console.log(
'Hi, my name is ' + this.name + '!'
);
}
}
myAnimal = new Animal('Bello');
myAnimal.say(); // Hi, my name is Bello!
38. var Animal, myAnimal;
Animal = function (name) {
this.name = name || 'Unknown';
}
Animal.prototype = {
say: function () {
console.log(
'Hi, my name is ' + this.name + '!'
);
}
}
myAnimal = new Animal('Bello');
myAnimal.say(); // Hi, my name is Bello!
39. var Animal, myAnimal;
Animal = function (name) {
this.name = name || 'Unknown';
}
Animal.prototype = {
say: function () {
console.log(
'Hi, my name is ' + this.name + '!'
);
}
}
myAnimal = new Animal('Bello');
myAnimal.say(); // Hi, my name is Bello!
40. var Animal, myAnimal;
Animal = function (name) {
this.name = name || 'Unknown';
}
Animal.prototype = {
say: function () {
console.log(
'Hi, my name is ' + this.name + '!'
);
}
}
myAnimal = new Animal('Bello');
myAnimal.say(); // Hi, my name is Bello!
41. var Animal, myAnimal;
Animal = function (name) {
this.name = name || 'Unknown';
}
Animal.prototype = {
say: function () {
console.log(
'Hi, my name is ' + this.name + '!'
);
}
}
myAnimal = new Animal('Bello');
myAnimal.say(); // Hi, my name is Bello!
44. var Animal = Backbone.Model.extend({
say: function () {}
});
var Dog = Animal.extend({
bark: function () {}
});
45. var Animal = Backbone.Model.extend({
say: function () {}
});
var Dog = Animal.extend({
bark: function () {}
});
46. var Animal = Backbone.Model.extend({
say: function () {}
});
var Dog = Animal.extend({
bark: function () {}
});
47. var Animal = Backbone.Model.extend({
say: function () {}
});
var Dog = Animal.extend({
bark: function () {}
});
48. var Animal = Backbone.Model.extend({
say: function () {}
});
var Dog = Animal.extend({
bark: function () {}
});
49. var Animal = Backbone.Model.extend({
say: function () {}
});
var Dog = Animal.extend({
bark: function () {}
});
50. var Dog = Animal.extend({
bark: function () {}
});
var myDog = new Dog({
name: 'Bello'
});
51. var Dog = Animal.extend({
bark: function () {}
});
var myDog = new Dog({
name: 'Bello'
});
52. // The self-propagating extend function that Backbone classes use.
var extend = function (protoProps, classProps) {
var child = inherits(this, protoProps, classProps);
child.extend = this.extend;
return child;
};
// Set up inheritance for the model, collection, and view.
Backbone.Model.extend = Backbone.Collection.extend =
Backbone.Router.extend = Backbone.View.extend = extend;
53. // The self-propagating extend function that Backbone classes use.
var extend = function (protoProps, classProps) {
var child = inherits(this, protoProps, classProps);
child.extend = this.extend;
return child;
};
// Set up inheritance for the model, collection, and view.
Backbone.Model.extend = Backbone.Collection.extend =
Backbone.Router.extend = Backbone.View.extend = extend;
54. // The self-propagating extend function that Backbone classes use.
var extend = function (protoProps, classProps) {
var child = inherits(this, protoProps, classProps);
child.extend = this.extend;
return child;
};
// Set up inheritance for the model, collection, and view.
Backbone.Model.extend = Backbone.Collection.extend =
Backbone.Router.extend = Backbone.View.extend = extend;
55. // The self-propagating extend function that Backbone classes use.
var extend = function (protoProps, classProps) {
var child = inherits(this, protoProps, classProps);
child.extend = this.extend;
return child;
};
// Set up inheritance for the model, collection, and view.
Backbone.Model.extend = Backbone.Collection.extend =
Backbone.Router.extend = Backbone.View.extend = extend;
56. // The self-propagating extend function that Backbone classes use.
var extend = function (protoProps, classProps) {
var child = inherits(this, protoProps, classProps);
child.extend = this.extend;
return child;
};
// Set up inheritance for the model, collection, and view.
Backbone.Model.extend = Backbone.Collection.extend =
Backbone.Router.extend = Backbone.View.extend = extend;
76. var AppRouter = Backbone.Router.extend({
routes: {
'': 'index',
'q?:query': 'query',
'show/:id': 'show'
},
index: function () {},
query: function (queryString) {},
show: function (id) {}
});
77. var AppRouter = Backbone.Router.extend({
routes: {
'': 'index',
'q?:query': 'query',
'show/:id': 'show'
},
index: function () {},
query: function (queryString) {},
show: function (id) {}
});
78. var AppRouter = Backbone.Router.extend({
routes: {
'': 'index',
'q?:query': 'query',
'show/:id': 'show'
},
index: function () {},
query: function (queryString) {},
show: function (id) {}
});
79. var AppRouter = Backbone.Router.extend({
routes: {
'': 'index',
'q?:query': 'query',
'show/:id': 'show'
},
index: function () {},
query: function (queryString) {},
show: function (id) {}
});
80. var AppRouter = Backbone.Router.extend({
routes: {
'': 'index',
'q?:query': 'query',
'show/:id': 'show'
},
index: function () {},
query: function (queryString) {},
show: function (id) {}
});
81. var AppRouter = Backbone.Router.extend({
routes: {
'': 'index',
'q?:query': 'query',
'show/:id': 'show'
},
index: function () {},
query: function (queryString) {},
show: function (id) {}
});
82. var AppRouter = Backbone.Router.extend({
routes: {
'': 'index',
'q?:query': 'query',
'show/:id': 'show'
},
index: function () {},
query: function (queryString) {},
show: function (id) {}
});
83. var AppRouter = Backbone.Router.extend({
routes: {
'': 'index',
'q?:query': 'query',
'show/:id': 'show'
},
index: function () {},
query: function (queryString) {},
show: function (id) {}
});
84. var AppRouter = Backbone.Router.extend({
routes: {
'': 'index',
'q?:query': 'query',
'show/:id': 'show'
},
index: function () {},
query: function (queryString) {},
show: function (id) {}
});
85. var AppRouter = Backbone.Router.extend({
routes: {
'': 'index',
'q?:query': 'query',
'show/:id': 'show'
},
index: function () {},
query: function (queryString) {},
show: function (id) {}
});
88. var AppRouter = Backbone.Router.extend({
// …
});
new AppRouter();
Backbone.history.start();
89. var AppRouter = Backbone.Router.extend({
// …
});
new AppRouter();
Backbone.history.start();
90. var AppRouter = Backbone.Router.extend({
// …
});
new AppRouter();
Backbone.history.start();
95. var Comment = Backbone.Model.extend({});
var newComment = new Comment({
name: 'Pierre Spring',
email: '[email protected]',
text: 'Hello World'
});
96. var Comment = Backbone.Model.extend({});
var newComment = new Comment({
name: 'Pierre Spring',
email: '[email protected]',
text: 'Hello World'
});
97. var Comment = Backbone.Model.extend({});
var newComment = new Comment({
name: 'Pierre Spring',
email: '[email protected]',
text: 'Hello World'
});
98. var Comment = Backbone.Model.extend({});
var newComment = new Comment({
name: 'Pierre Spring',
email: '[email protected]',
text: 'Hello World'
});
99. var Comment = Backbone.Model.extend({});
var newComment = new Comment({
name: 'Pierre Spring',
email: '[email protected]',
text: 'Hello World'
});
100. var Comment = Backbone.Model.extend({});
var newComment = new Comment({
name: 'Pierre Spring',
email: '[email protected]',
text: 'Hello World'
});
101. var newComment = new Comment({
text: 'Hello World'
});
newComment.set({
name: 'Pierre Spring',
email: '[email protected]'
});
newComment.get('name'); // Pierre Spring
102. var newComment = new Comment({
text: 'Hello World'
});
newComment.set({
name: 'Pierre Spring',
email: '[email protected]'
});
newComment.get('name'); // Pierre Spring
103. var newComment = new Comment({
text: 'Hello World'
});
newComment.set({
name: 'Pierre Spring',
email: '[email protected]'
});
newComment.get('name'); // Pierre Spring
104. var newComment = new Comment({
text: 'Hello World'
});
newComment.set({
name: 'Pierre Spring',
email: '[email protected]'
});
newComment.get('name'); // Pierre Spring
105. var Comment = Backbone.Model.extend({});
var newComment = new Comment({ /* … */ });
newComment.save();
106. var Comment = Backbone.Model.extend({
url: '/api/comment/'
});
var newComment = new Comment({ /* … */ });
newComment.save();
108. var Comment = Backbone.Model.extend({
localStorage: new Store("comment")
});
var newComment = new Comment({ /* … */ });
newComment.save();
109. var Comment = Backbone.Model.extend({
localStorage: new Store("comment"),
initialize: function (options) {
}
});
var newComment = new Comment({ /* … */ });
newComment.save();
110. var Comment = Backbone.Model.extend({
localStorage: new Store("comment"),
initialize: function (options) {
if (!options.date) {
this.set({
date: moment().toString()
});
}
}
});
var newComment = new Comment({ /* … */ });
newComment.save();
111. var Comment = Backbone.Model.extend({
localStorage: new Store("comment"),
initialize: function (options) {
if (!options.date) {
this.set({
date: moment().toString()
});
}
}
});
var newComment = new Comment({ /* … */ });
newComment.save();
113. var Comment = Backbone.Model.extend({
localStorage: new Store("comment"),
initialize: function (options) {
if (!options.date) {
this.set({
date: moment().toString()
});
}
}
});
var newComment = new Comment({ /* … */ });
newComment.save();
114. var Comment = Backbone.Model.extend({
localStorage: new Store("comment"),
initialize: function (options) {
if (!options.date) {
this.set({
date: moment().toString()
});
}
},
getDisplayDate: function () {
var d = this.get('date');
return moment(d).fromNow();
}
});
115. var Comment = Backbone.Model.extend({
localStorage: new Store("comment"),
initialize: function (options) {
if (!options.date) {
this.set({
date: moment().toString()
});
}
},
getDisplayDate: function () {
var d = this.get('date');
return moment(d).fromNow();
}
});
116. var Comment = Backbone.Model.extend({
localStorage: new Store("comment"),
initialize: function (options) {
if (!options.date) {
this.set({
date: moment().toString()
});
}
},
getDisplayDate: function () {
var d = this.get('date');
return moment(d).fromNow();
}
});
117. var Comment = Backbone.Model.extend({
localStorage: new Store("comment"),
initialize: function (options) {
if (!options.date) {
this.set({
date: moment().toString()
});
}
},
getDisplayDate: function () {
var d = this.get('date');
return moment(d).fromNow();
}
});
118. var newComment = new Comment({ /* … */ });
newComment.getDisplayDate();
// a few seconds ago
181. var commentCollection = new commentCollection();
var commentView = new CommentView({
commentCollection: commentCollection
});
$('.list-view').html(commentView.el);
commentCollection.fetch();
182. var commentCollection = new commentCollection();
var commentView = new CommentView({
commentCollection: commentCollection
});
$('.list-view').html(commentView.el);
commentCollection.fetch();
183. var commentCollection = new commentCollection();
var commentView = new CommentView({
commentCollection: commentCollection
});
$('.list-view').html(commentView.el);
commentCollection.fetch();
184. var commentCollection = new commentCollection();
var commentView = new CommentView({
commentCollection: commentCollection
});
$('.list-view').html(commentView.el);
commentCollection.fetch();
185. var CommentFormView = Backbone.View.extend({
events: {
'submit form': 'newComment'
},
initialize: function (options) { /* … */ },
render: function () { /* … */ },
newComment: function (e) {
var options, comment;
e.preventDefault();
options = {};
_.each(
this.$('form').serializeArray(),
function (field) {
options[field.name] = field.value;
}
);
this.commentCollection.create(options);
this.$('input, textarea').val('');
}
});
186. var CommentFormView = Backbone.View.extend({
events: {
'submit form': 'newComment'
},
initialize: function (options) { /* … */ },
render: function () { /* … */ },
newComment: function (e) {
var options, comment;
e.preventDefault();
options = {};
_.each(
this.$('form').serializeArray(),
function (field) {
options[field.name] = field.value;
}
);
this.commentCollection.create(options);
this.$('input, textarea').val('');
}
});
187. var CommentFormView = Backbone.View.extend({
events: {
'submit form': 'newComment'
},
initialize: function (options) { /* … */ },
render: function () { /* … */ },
newComment: function (e) {
var options, comment;
e.preventDefault();
options = {};
_.each(
this.$('form').serializeArray(),
function (field) {
options[field.name] = field.value;
}
);
this.commentCollection.create(options);
this.$('input, textarea').val('');
}
});
188. var CommentFormView = Backbone.View.extend({
events: {
'submit form': 'newComment'
},
initialize: function (options) { /* … */ },
render: function () { /* … */ },
newComment: function (e) {
var options, comment;
e.preventDefault();
options = {};
_.each(
this.$('form').serializeArray(),
function (field) {
options[field.name] = field.value;
}
);
this.commentCollection.create(options);
this.$('input, textarea').val('');
}
});
189. var CommentFormView = Backbone.View.extend({
events: {
'submit form': 'newComment'
},
initialize: function (options) { /* … */ },
render: function () { /* … */ },
newComment: function (e) {
var options, comment;
e.preventDefault();
options = {};
_.each(
this.$('form').serializeArray(),
function (field) {
options[field.name] = field.value;
}
);
this.commentCollection.create(options);
this.$('input, textarea').val('');
}
});
190. var CommentFormView = Backbone.View.extend({
events: {
'submit form': 'newComment'
},
initialize: function (options) { /* … */ },
render: function () { /* … */ },
newComment: function (e) {
var options, comment;
e.preventDefault();
options = {};
_.each(
this.$('form').serializeArray(),
function (field) {
options[field.name] = field.value;
}
);
this.commentCollection.create(options);
this.$('input, textarea').val('');
}
});
191. var CommentFormView = Backbone.View.extend({
events: {
'submit form': 'newComment'
},
initialize: function (options) { /* … */ },
render: function () { /* … */ },
newComment: function (e) {
var options, comment;
e.preventDefault();
options = {};
_.each(
this.$('form').serializeArray(),
function (field) {
options[field.name] = field.value;
}
);
this.commentCollection.create(options);
this.$('input, textarea').val('');
}
});
192. var CommentFormView = Backbone.View.extend({
events: {
'submit form': 'newComment'
},
initialize: function (options) { /* … */ },
render: function () { /* … */ },
newComment: function (e) {
var options, comment;
e.preventDefault();
options = {};
_.each(
this.$('form').serializeArray(),
function (field) {
options[field.name] = field.value;
}
);
this.commentCollection.create(options);
this.$('input, textarea').val('');
}
});
193. var CommentFormView = Backbone.View.extend({
events: {
'submit form': 'newComment'
},
initialize: function (options) { /* … */ },
render: function () { /* … */ },
newComment: function (e) {
var options, comment;
e.preventDefault();
options = {};
_.each(
this.$('form').serializeArray(),
function (field) {
options[field.name] = field.value;
}
);
this.commentCollection.create(options);
this.$('input, textarea').val('');
}
});
194. var CommentFormView = Backbone.View.extend({
events: {
'submit form': 'newComment'
},
initialize: function (options) { /* … */ },
render: function () { /* … */ },
newComment: function (e) {
var options, comment;
e.preventDefault();
options = {};
_.each(
this.$('form').serializeArray(),
function (field) {
options[field.name] = field.value;
}
);
this.commentCollection.create(options);
this.$('input, textarea').val('');
}
});