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.
- student = Backbone.Model.extend({
- defaults:{
- name:"shris"
- },
- initialize: function () {
- this.bindEvent();
-
- },
- bindEvent: function(){
- this.on("change: shris", function (model) {
- var Fname=model.get("name");
- alert("Hello this name changed to"+ Fname);
- });
- }
- });
- $(document).ready(function () {
- var std = new student();
- std.set({ name: "Firstname" });
- });
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.
- student.on("change", function() {
- if (student.hasChanged("attr")) {
- ...
- }
- });
- 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.
- var Student = Backbone.Model.extend({ urlRoot: '/student' });
-
- var shris = new Student({ id: "Hello2015" });
-
- 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.
- var Student = new Backbone.Model({
- name: "Shris"
- });
-
- Student.on("change:name", function (model, name) {
- alert("Changed name from " + Student.previous("name") + " to " + name);
- });
-
- 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.