69
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:
Index | Method | Description |
---|---|---|
1. | extend | It extends Backbone.model class while creating your own backbone model. |
2. | initialize | When model instance is created, the class’s constructor gets called and it is invoked By defining initialize function when model is created. |
3. | get | It gets value of an attribute on the model. |
4. | set | It sets the value of an attribute in the model. |
5. | escape | It is similar to get function, but returns the html-escaped version of a model’s attribute. |
6. | has | It returns true, if attribute value defined with non-null value or non-undefined value. |
7. | unset | It removes an attribute from a backbone model. |
8. | clear | It removes all attributes, including id attribute from a backbone model. |
9. | id | It is used to identify model entity uniquely.It can be set manually when model is created or populated and saved on the server. |
10. | idattribute | It defines the model’s unique identifier which contains the name of the class member that will be used as id. |
11. | cid | It is auto generated client id by backbone which uniquely identify the model on the client. |
12. | attributes | It is used to define the property of a model. |
13. | changed | It changes all the attributes that have changed after setting the attributes using set() method. |
14. | defaults | It 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. | toJSON | It returns copy of the attributes as an object for JSON stringification. |
16. | sync | It is used to communicate with the server and to represents state of a model. |
17. | fetch | It accepts the data from the server by delegating sync() method in the model. |
18. | save | It saves the data of the model by delegating to sync() method which reads and save the model every time when backbone calls it. |
19. | destroy | It is used to destroy or remove the model from the server by using the Backbone.sync method which releases http “delete” request. |
20. | validate | If input is invalid, it returns specified error message or if input is valid, it doesn’t specify anything and simply display the result. |
21. | validationError | It display validation error, if validation fails or after the invalid event is triggered. |
22. | isValid | It checks the model state by using validate() method and also checks validations for each attributes. |
23. | url | It is used for the instance of the model and returns URL where model’s resource is located. |
24. | urlRoot | It enables the URL function by using the model id to generate the URL. |
25. | parse | The returns the model?s data by passing through the response object and represents the data in JSON format. |
26. | clone | It is used to create deep copy of a model or to copy one model object to another object. |
27. | hasChanged | It returns TRUE, if the attributes have changed since the last set. |
28. | isNew | It determines whether the model is a new or existing one. |
29. | changedAttributes | It returns the model’s attributes that have changed since the last set or becomes false, if there is no attribute. |
30. | previous | It determines the previous value of the changed attribute. |
31. | previousAttributes | It returns state of the all attributes prior to last change event. |
Next TopicBackbone.js Model.extend()