Methods/Attributes in Backbone Model: Part 4

Some useful methods/attributes in Backbone.Model

This article is part of a continued series of articles about Backbone.JS. I recommend you visit my previous articles also.

Here we go.
  • clone model.clone()

    returns a new instance of the model.

  • changed

    The changed property contains all the attributes that have changed since last set.State and is internally maintained by set. Therefore we do not update the changed directly.
    1. student = Backbone.Model.extend({  
    2. defaults:{  
    3. name:"shris"  
    4. },  
    5. initialize: function () {  
    6. this.bindEvent();  
    8. },  
    9. bindEvent: function(){  
    10. this.on("change: shris"function (model) {  
    11. var Fname=model.get("name");  
    12. alert("Hello this name changed to"+ Fname);  
    13. });  
    14. }  
    15. });  
    16. $(document).ready(function () {  
    17. var std = new student();  
    18. std.set({ name: "Firstname" });  
    19. });  

    alert output:

  • hasChanged

    It has the model changed since the last set. If we ed an attribute, hasChanged will return true if that attributed has changed. hasChanged is specially useful for a "change" event.
    1. student.on("change"function() {  
    2. if (student.hasChanged("attr")) {  
    3. ...  
    4. }  
    5. });  

  • changedAttributes

    retrieves those attributes that has changed since the last set.

  • URL

    After we set or save a model resource, this will be located on the server. model.url() will return the relative URL of that resource on the server. We can add logic for the models, if the models are located somewhere else. We can override by pointing to an explicit urlRoot, by default model.url() that will generate an URL like "[collection.url]/[id]".

  • urlRoot
    model.urlRoot or model.urlRoot()

    If when we are using a model outside the collection, we use urlRoot then that will generate URLs based on model id, "[urlRoot]/id" by default URL function.
    1. var Student = Backbone.Model.extend({ urlRoot: '/student' });  
    3. var shris = new Student({ id: "Hello2015" });  
    5. alert(shris.url());  

  • isNew

    This will check whether the model has been saved to the server yet or not. The Model is considered as new if it does not have an id.

  • previous

    This method is quite useful during a "change" event. It is used to get the value of the changed attributes.
    1. var Student = new Backbone.Model({  
    2. name: "Shris"  
    3. });  
    5. Student.on("change:name"function (model, name) {  
    6. alert("Changed name from " + Student.previous("name") + " to " + name);  
    7. });  
    9. Student.set({ name: "Shridhar" });  
    alert output:

  • previousAttributes

    will return a copy of the model's previous attributes.

    Here are all the methods and attributes with the default declaration depending on Backbone.JS:

    Some other methods/attributes


    In this article of the series, we learned about all the possible terms related to Backbone.model. This last tutorial is related to Backbone.model. In the next tutorials, I will try to explain other remaining modules of Backbone.JS. Comments are welcome.

Up Next
    Ebook Download
    View all
    View all