Sync of backbone Learning

Source: Internet
Author: User

SyncThis is the function that the Backbone calls every time it reads or saves the model to the server. By default, it uses(JQuery/Zepto). ajaxMethod Request. If you want to use other solutions, such as WebSockets, XML, or Local Storage, we can reload this function to meet our needs.

The Code logic is analyzed as follows:

// Backbone. sync // ------------- // Override this function to change the manner in which Backbone persists // models to the server. you will be passed the type of request, and the // model in question. by default, makes a RESTful Ajax request // to the model's url ()'. some possible mizmizations cocould be: // * Use 'settimeout' to batch rapid-fire updates into a single request. // * Send up th E models as XML instead of JSON. // * Persist models via WebSockets instead of Ajax. /// Turn on 'backbone. emulateHTTP 'in order to send 'put' and 'delete' requests // as 'post', with A' _ method' parameter containing the true HTTP method, // as well as all requests with the body as 'application/x-www-form-urlencoded' // instead of 'Application/json' with the model in a param named' model '. // Us Eful when interfacing with server-side extensions ages like ** PHP ** that make // it difficult to read the body of 'put' requests. backbone. sync = function (method, model, options) {var type = methodMap [method]; // Default options, unless specified. _. defaults (options | (options = {}), {emulateHTTP: Backbone. emulateHTTP, emulateJSON: Backbone. emulateJSON}); // Default JSON-request options. var params = {Type: type, dataType: 'json'}; // Ensure that we have a URL. // make sure that a url is not included in the option to obtain the if (! Options. url) {params. url = _. result (model, 'url') | urlError ();} // Ensure that we have the appropriate request data. if (options. data = null & model & (method = 'create' | method = 'update' | method = 'patch ') {params. contentType = 'application/json'; params. data = JSON. stringify (options. attrs | model. toJSON (options);} // For older servers, emulate JSON by encoding the request I Nto an HTML-form. if (options. emulateJSON) {params. contentType = 'application/x-www-form-urlencoded'; params. data = params. data? {Model: params. data }:{};}// For older servers, emulate HTTP by mimicking the HTTP method with '_ method' // And an 'X-HTTP-method-override' header. if (options. emulateHTTP & (type = 'put' | type = 'delete' | type = 'patch ') {params. type = 'post'; if (options. emulateJSON) params. data. _ method = type; // handle var beforeSend = options before the request. beforeSend; options. beforeSend = function (xhr) {Xhr. setRequestHeader ('x-HTTP-Method-override', type); if (beforeSend) return beforeSend. apply (this, arguments) ;};}// Don't process data on a non-GET request. if (params. type! = 'Get '&&! Options. emulateJSON) {params. processData = false;} // If we're sending a 'patch 'request, and we're in an old Internet Explorer // that still has ActiveX enabled by default, override jQuery to use that // for XHR instead. remove this line when jQuery supports 'patch 'on IE8. // if (params. type = 'patch '& window. activeXObject &&! (Window. external & window. external. msActiveXFilteringEnabled) {params. xhr = function () {return new ActiveXObject ("Microsoft. XMLHTTP ") ;};}// Make the request, allowing the user to override any Ajax options. var xhr = options. xhr = Backbone. ajax (_. extend (params, options); model. trigger ('request', model, xhr, options); // triggers the request return xhr;}; // Map from CRUD to HTTP for our default 'backbone. sync 'Implementation. var methodMap = {'create': 'post', 'update': 'put', 'patch ': 'patch', 'delete': 'delete', 'read ': 'get'}; // Set the default implementation of 'backbone. ajax 'to proxy through '$ '. // Override this if you 'd like to use a different library. // ajax request Backbone of jquery zepto by default. ajax = function () {return Backbone. $. ajax. apply (Backbone. $, arguments );};

 

Guidance, error correction, and suggestions are welcomed.

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.