Analysis of Backbone model layer

Source: Internet
Author: User
Tags tojson

The model layer has two classes: model, Collection

1.Model

Without looking through the documents, we speak in code.

The next class is analyzed first.
var MyM = Backbone.Model.extend ({})//Constructs a Model class, MyM

This class is incredibly empty, there is no official said so many attributes ah, is underscore failure?

>_.keys (MyM) ["Extend", "__super__"]>_.keys (Backbone.model) ["Extend"]

By default, the sub-analogy parent has a __super__, what is this?

>_.isobject (mym.__super__) True

Object. Under observation:

>_.keys (mym.__super__) ["On", "Once", "off", "trigger", "stoplistening", "Listento", "listentoonce", "bind", "unbind "," changed "," ValidationError "," Idattribute "," Initialize "," ToJSON "," Sync "," get "," Escape "," has "," set "," unset "," C Lear "," hasChanged "," Changedattributes "," previous "," Previousattributes "," Fetch "," save "," Destroy "," url "," Parse ", "Clone", "IsNew", "IsValid", "_validate", "Keys", "values", "pairs", "invert", "pick", "omit"]

Conclusion: A pointer to the parent class? The interface (functions/events/attributes) listed in the official documentation appears, as above.

Compare the synchronization of relational data to see the implementation of fetch:

>mym.__super__.fetch    function (t) {    T=t?i.clone (t): {};    if (t.parse===void 0) t.parse=true;      var e=this;    var r=t.success;    T.success=function (i) {        if (!e.set (E.parse (i,t), T)) return false;        if (r) r (e,i,t);        E.trigger ("Sync", e,i,t)    };    Q (this,t);    Return This.sync ("read", This,t)}

Do not understand what Q is, you can see, Mym.fetch ({success: function (e,i,t) {}), that is, the successful callback function accepts three parameters, what is unknown.

The second is that fetch calls itself the sync function.

Foggy A

---------------Split Line-------------------------

An instance of the parsing class.

Constructs an instance of the Mym class and analyzes:

>m_obj= new Mym;s {cid: "C6", Attributes:object, _changing:false, _previousattributes:object, Changed:object ...} >_.isobject (m_obj) True>_.keys (m_obj) ["CID", "Attributes", "_changing", "_previousattributes", "Changed", "_ Pending "]

As you can see, the properties of the instance are very different from the properties of the class and seem to have no relation to the properties of the parent class (no inheritance?). )。

Is that really the case?

>_.functions (m_obj) ["_validate", "bind", "changedattributes", "clear", "clone", "constructor", "destroy", "Escape" , "Fetch", "get", "has", "hasChanged", "Initialize", "invert", "isnew", "IsValid", "Keys", "Listento", "listentoonce", "of F "," omit "," on "," once "," pairs "," parse "," Pick "," previous "," Previousattributes "," Save "," Set "," Stoplistening "," syn C "," ToJSON "," Trigger "," unbind "," unset "," url "," values "]

Well, this has a certain intersection with mym.__super__. The intersection is:

>m_obj_funcs = _.functions (m_obj) Base_funcs = _.functions (mym.__super__) _.filter (Base_funcs, function (f) {return _ . Contains (M_OBJ_FUNCS,F)}) ["_validate", "bind", "changedattributes", "clear", "clone", "Destroy", "Escape", "Fetch", "  Get "," has "," hasChanged "," Initialize "," invert "," isnew "," IsValid "," Keys "," Listento "," listentoonce "," Off "," omit ", "On", "once", "pairs", "parse", "Pick", "previous", "Previousattributes", "Save", "Set", "Stoplistening", "Sync", "ToJSON "," Trigger "," unbind "," unset "," url "," values "]

What are the functions of m_obj?

>_.filter (M_obj_funcs, function (f) {return!_.contains (base_funcs,f);}) ["Constructor"]

Seems to be a constructor

Property

["CID", "Attributes", "_changing", "_previousattributes", "changed", "_pending"]

(Hint: Go back to the section of constructing m_obj before.) )

First,attributes is a collection of attribute key-value pairs (JSON objects), for example,

>m_obj.set ({' Name ': ' Tommy ', ' blog ': ' Http://www.cnblogs.com/Tommy-Yu '}) >m_obj.attributesobject {name: "Tommy ", Blog:" Http://www.cnblogs.com/Tommy-Yu "}

Second,changed is also a JSON object that records which properties of the instance have been changed---only the part of the modification is recorded. As follows:

>m_obj.set ({' name ': ' Joe '}) >m_obj.changedobject {name: "Joe"}

_previousattributes, record the object before the change

>m_obj._previousattributesobject {name: "Tommy", Blog: "Http://www.cnblogs.com/Tommy-Yu"}

As for the CID , a temporary ID. The objects that are automatically assigned to a class (such as MYM) are numbered in numerical order.

The last is changing.

Function

["_validate", "bind", "changedattributes", "clear", "clone", "Destroy", "Escape", "Fetch", "get", "has", "hasChanged", "I Nitialize "," invert "," isnew "," IsValid "," Keys "," Listento "," listentoonce "," Off "," omit "," on "," once "," pairs "," parse "," Pick "," previous "," Previousattributes "," Save "," Set "," Stoplistening "," Sync "," ToJSON "," Trigger "," unbind "," Unse T "," url "," values "]

Given the limited space, the highlights are:

--------------Small Split------------

The set () ---has been demonstrated above, setting the Attributes property of the object.

Has (attr) --Determines whether an object's attributes has attr properties

>m_obj.has (' name ') True>m_obj.has (' age ') false

get (attr) --Gets the value of one key in the Attributes property of the object:

>m_obj.get (' name ') ' Tommy '

-------------Small Split-------------

previousattributes () --- the Get encapsulation of the _previousattributes property

>m_obj.previousattributes () Object {name: "Tommy", Blog: "Http://www.cnblogs.com/Tommy-Yu"}

Previous (key) --equivalent to _previousattributes[key]

>m_obj.previous (' name ') ' Tommy '

-------------Small Split-------------

ToJSON () --Convert to JSON object

>m_obj.tojson () Object {name: "Tommy", Blog: "Http://www.cnblogs.com/Tommy-Yu"}

Keys ()-- convert keys to arrays

>m_obj.keys () ["Name", "blog"]

Values () --Convert values to arrays

M_obj.values () ["Tommy", "Http://www.cnblogs.com/Tommy-Yu"]

-------------Small Split-------------

isnew () --Whether the client creates a new object (used when synchronizing with the server)

>m_obj.isnew () True

isvalide ()--whether the object passed validation

-------------Small Split-------------

CRUD operations: Fetch/save/destroy

Prerequisite: Configure the URL(String/funciton--and Ajax Path)

Analysis of Backbone model layer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.