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()

    model.clone()
    returns a new instance of the model.

  • changed
    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();  
    7.   
    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
    model.hasChanged([attribute])

    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
    model.changedAttributes([attributes])

    changedAttributes
    retrieves those attributes that has changed since the last set.

  • URL
    model.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' });  
    2.   
    3. var shris = new Student({ id: "Hello2015" });  
    4.   
    5. alert(shris.url());  

  • isNew
    model.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
    model.previous(attribute)

    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. });  
    4.   
    5. Student.on("change:name"function (model, name) {  
    6. alert("Changed name from " + Student.previous("name") + " to " + name);  
    7. });  
    8.   
    9. Student.set({ name: "Shridhar" });  
    alert output:




  • previousAttributes
    model.previousAttributes()

    model.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



    Summary

    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
    Learn
    View all