📜  BackboneJS-模型

📅  最后修改于: 2020-10-27 03:40:11             🧑  作者: Mango


模型包含动态数据及其逻辑。诸如转换,验证,计算的属性和访问控制之类的逻辑属于“模型”类别。由于模型包含所有应用程序数据,因此也称为JavaScript应用程序心脏

下表列出了可用于操作BackboneJS-Model的所有方法-

S.No. Methods & Description
1 extend

It extends the backbone.Model class while creating your own backbone Model.

2 initialize

When a model instance is created, the class’s constructor gets called and it is invoked by defining the initialize function when the model is created.

3 get

It gets the value of an attribute on the model.

4 set

It sets the value of an attribute in the model.

5 escape

It is like the get function, but returns the HTML-escaped version of a model’s attribute.

6 has

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

Removes all attributes, including id attribute from a backbone model.

9 id

It uniquely identifies the model entity, that might be manually set when a model is created or populated or when a model is saved on the server.

10 idAttribute

Defines a model’s unique identifier which contains the name of the member of the class which will be used as id.

11 cid

It is an auto generated client id by Backbone which uniquely identifies the model on the client.

12 attributes

Attributes defines property of a model.

13 changed

Changes all the attributes that have changed after setting the attributes using the set() method.

14 defaults

Sets a default value to a model, that means if the user doesn’t specify any data, the model won’t fall with an empty property.

15 toJSON

Returns a copy of the attributes as an object for JSON stringification.

16 sync

It is used to communicate with the server and to represent the state of a model.

17 fetch

Accept the data from the server by delegating sync() method in the model.

18 save

Saves the data of the model by delegating to sync() method which reads and saves the model every time when a Backbone calls it.

19 destroy

Destroys or removes the model from the server by using the Backbone.sync method which delegates the HTTP “delete” request.

20 validate

If the input is invalid, it returns a specified error message or if the input is valid, it doesn’t specify anything and simply displays the result.

21 validationError

It displays the validation error, if the validation fails or after the invalid event is triggered.

22 isValid

It checks the model state by using the validate() method and also checks validations for each attribute.

23 url

It is used for the instance of the model and returns the url to where the model’s resource is located.

24 urlRoot

Enables the url function by using the model id to generate the URL.

25 parse

Returns the model’s data by passing through the response object and represents the data in the JSON format.

26 clone

It is used to create a deep copy of a model or to copy one model object to another object.

27 hasChanged

Returns true, if the attribute gets changed since the last set.

28 isNew

Determines whether the model is a new or an existing one.

29 changedAttributes

It returns the model’s attributes that have changed since the last set or else becomes false, if there are no attributes.

30 previous

It determines the previous value of the changed attribute.

31 previousAttributes

Returns the state of the all the attributes prior to the last change event.

下划线方法

有六个Underscore.js方法,提供了在Backbone.Model上使用的功能。

S.No. Methods & Description
1

_.keys(object)

It is used to access the object’s enumerable properties.

2

_.values(object)

It is used to get values of object’s properties.

3

_.pairs(object)

It describes the object’s properties in terms of key value pairs.

4

_.invert(object)

It returns the copy of object, in which keys have become the values and vice versa.

5

_.pick(object, *keys)

It returns the copy of object and indicates which keys to pick up.

6

_.omit(object, *keys)

It returns the copy of object and indicates which keys to omit.