Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Backbone Model And Collection

var Song = Backbone.Model.extend();
var Songs = Backbone.Collection.extend({model: Song})
Comment

Backbone Collection


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);
Comment

Backbone.js collection.create()

.create() makes a new instance of the model in the Collection.

     var CollectionDemo = Backbone.Collection.extend({  
           model : ModelDemo   //The model 'ModelDemo' is specified by overriding the 'model' property  
        });  
  
  
  
     var collectiondemo = new CollectionDemo();  
              collectiondemo.create({  
                 Name:"John Smith",  
                 Country:"Mars"  
              });  
           }  
        });  
Comment

Backbone Collection Example

/*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"}])
Comment

PREVIOUS NEXT
Code Example
Javascript :: js check that interactive element is not focused 
Javascript :: c# to javascript object 
Javascript :: array of objects javascript 
Javascript :: event 
Javascript :: set to array js 
Javascript :: data.json 
Javascript :: jsdoc default value 
Javascript :: clear console javascript 
Javascript :: fs 
Javascript :: js 
Javascript :: dynamic styles in react native 
Javascript :: js return 
Javascript :: regex in javascript 
Javascript :: how to remove duplicates in js 
Javascript :: lodash sum array of objects 
Javascript :: ajax get request javascript 
Javascript :: url enocde in javascript 
Javascript :: javascript neue zeilekill 
Javascript :: p cannot appear as a descendant of p react 
Javascript :: javascript function expressions 
Javascript :: javascript copy array using spread operator 
Javascript :: javascript Assigning to a getter-only property is not allowed 
Javascript :: actionscript random randomfunction 
Javascript :: ex: javascript Executor 
Javascript :: javascript copy by reference 
Javascript :: Cntrlsss:$.Control-Ai 
Javascript :: phaser animation on repeat event 
Javascript :: toast waning 
Javascript :: string variable 
Javascript :: mongo db backup node js daily 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =