Home » Backbone.js Model

Backbone.js Model

Backbone.js models are the most important building blocks used to build backbone.js applications. It is also known as the heart of the JavaScript application. Model contains dynamic data and its logic. It performs various types of action on the data like validation, conversion, computed properties, access control etc.

Following is a list of methods that can be used to manipulate the Backbone.js Model:

1.extendIt extends Backbone.model class while creating your own backbone model.
2.initializeWhen model instance is created, the class’s constructor gets called and it is invoked
By defining initialize function when model is created.
3.getIt gets value of an attribute on the model.
4.setIt sets the value of an attribute in the model.
5.escapeIt is similar to get function, but returns the html-escaped version of a model’s attribute.
6.hasIt returns true, if attribute value defined with non-null value or non-undefined value.
7.unsetIt removes an attribute from a backbone model.
8.clearIt removes all attributes, including id attribute from a backbone model.
9.idIt is used to identify model entity uniquely.It can be set manually when model is created or populated and saved on the server.
10.idattributeIt defines the model’s unique identifier which contains the name of the class member that will be used as id.
11.cidIt is auto generated client id by backbone which uniquely identify the model on the client.
12.attributesIt is used to define the property of a model.
13.changedIt changes all the attributes that have changed after setting the attributes using set() method.
14.defaultsIt sets a default value to a model and simply states that if the user doesn’t specify any data, the model won’t fall with empty property.
15.toJSONIt returns copy of the attributes as an object for JSON stringification.
16.syncIt is used to communicate with the server and to represents state of a model.
17.fetchIt accepts the data from the server by delegating sync() method in the model.
18.saveIt saves the data of the model by delegating to sync() method which reads and save the model every time when backbone calls it.
19.destroyIt is used to destroy or remove the model from the server by using the Backbone.sync method which releases http “delete” request.
20.validateIf input is invalid, it returns specified error message or if input is valid, it doesn’t specify anything and simply display the result.
21.validationErrorIt display validation error, if validation fails or after the invalid event is triggered.
22.isValidIt checks the model state by using validate() method and also checks validations for each attributes.
23.urlIt is used for the instance of the model and returns URL where model’s resource is located.
24.urlRootIt enables the URL function by using the model id to generate the URL.
25.parseThe returns the model?s data by passing through the response object and represents the data in JSON format.
26.cloneIt is used to create deep copy of a model or to copy one model object to another object.
27.hasChangedIt returns TRUE, if the attributes have changed since the last set.
28.isNewIt determines whether the model is a new or existing one.
29.changedAttributesIt returns the model’s attributes that have changed since the last set or becomes false, if there is no attribute.
30.previousIt determines the previous value of the changed attribute.
31.previousAttributesIt returns state of the all attributes prior to last change event.

You may also like