How to use backbone.js

User :
To declare User model :
App.Backbone.User = Backbone.Model.extend();
 To declare User Collection :
  1. we assign the model.
  2.  we bind the add and remove event to collection
 To declare User Collection :
App.Backbone.Users = Backbone.Collection.extend({ 
    model : App.User,
    initialize: function(models, args) {
       this.bind('add', this.renderone);
       this.bind('remove', this.destroy); },
    renderone:function(user){
        var view = new App.Backbone.UserView({model: user});
        view.render();
        var view = new App.Backbone.UserCardView({model: user});
       view.render(); 
    },
   destroy:function(user){
        $(user.view.el).remove();
    }
});

To initialise the Collection    of user

App.Users = new App.Backbone.Users();
To add user in collection.
var data ={id:'rahul', username:rahul,firstname:'Rahul'}; 
App.Users.add(data);

//at this point automatically view will be rendered as we bind the add event in collection above.

To remove a model
App.Users.remove(App.Users.get('rahul'));
// so on remove view will also remove
To update a model
var data ={id:'rahul', username:rahul,firstname:'Rahul',lastname:'mehta'};
App.Users.get('rahul').set(data);
the view also render due to change event in view.
Thanks


Advertisements

About rahul23134654

Hi, I am Rahul Meha , B.E. in (I.T.)
This entry was posted in UI Js. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s