var Song = Backbone.Model.extend();
var Songs = Backbone.Collection.extend({model: Song})
var Song = Backbone.Model.extend();
var Songs = Backbone.Collection.extend({
model:Song});
var songs = new Songs([new Song({title: "Song 1"}),
new Song({title: "Song 2"}),
new Song({title: "Song 3"})
]);
songs.add(new Song({title:"Song 4"}));
var firstSong = songs.at(0);
var songWithIdC1 = songs.get("c1");
songs.remove(firstSong);
console.log(songs);
/*you must specify collection: in the parameter, you cannot use another in place of this.collection */
var Person = Backbone.Model.extend({});
var PersonsCollection = Backbone.Collection.extend({});
var ViewDemo = Backbone.View.extend({
el: $('#myContent'),
template: _.template("<b> <%= name %></b>"),
initialize: function(){
this.render();
},
render: function(){
for(var i =0; i<this.collection.models.length; i++)
{
this.$el.append(this.template( this.collection.models[i].toJSON()));
}
}
});
const personscollection = new PersonsCollection();
personscollection.add([{name:"This is my name 1"}])
personscollection.add([{name:"This is my name 2"}])
personscollection.add([{name:"This is my name 3"}])
personscollection.add([{name:"This is my name 4"}])