Backbone.js Tutorial Nine: Backbone Utility library

Source: Internet
Author: User
Tags functions log reference return version cloudflare

Use Backbone.noconflict () to store and create a special (that is, custom namespace) references to backbone

When backbone is parsed by a browser, the first thing backbone does is to store a value that is referenced to the backbone contained in the global scope (that is, window.backbone). This is because backbone rewrites or occupies the namespace and wants to give developers the opportunity to store the initial values used before backbone is parsed. This is the time when Backbone.noconflict () is working. Calling Backbone.noconflict () will return the backbone namespace to the original owner, and then return a reference to the newly resolved backbone.js version, so you can create a new namespace for it. In the following code, I created a virtual backbone version before backbone.js load. And when giving backbone a new namespace for the newly resolved version, use Backbone.noconflict () to ensure that this version retains backbone references.

 
  
  

Tips:

    • If you call Backbone.noconflict () but do not store the reference value returned by the callback function, you will lose a reference to the browser to resolve the latest Backbone.js code.
 
  
   
  1. /* Store the initial reference to the backbone*/before loading the latest backbone.js version
  2. var lastbackboneparsed = backbone.noconflict ();
  3. To verify that backbone in the global scope point to the original version
  4. Console.log (backbone["I ' m the previous owner of backbone in the global scope"]); Logs true
  5. /* Verify that Backbone.js is included in the new lastbackboneparsed namespace, not in the backbone namespace * *
  6. Console.log (lastbackboneparsed.version)//logs 1.0.0

Using backbone.$ to allocate DOM and XHR libraries

Backbone can automatically and sequentially use any version of jquery, Zepto, or ender that is found in the global scope, for DOM processing and XHR (also known as Ajax), by storing a reference to one of these libraries in backbone.$. Reference instructions.

If these libraries are not found in the global scope, backbone attempts to use any value that can be found in the global scope to $. If you do not include any of the previously mentioned libraries, and you do not use $ as a namespace or other name to define your own library, it will be undefined.

When backbone is parsed, you can also manually provide backbone.$ with a value. In the following code, I show two ways to set the backbone.$.

 
   
  1. in the case of JSON

Summary

I've read and browsed a lot of tutorials on backbone.js, and no one can elaborate on exactly what Backbone.model, backbone.collection, and Backbone.view functions are for. So I wrote this series of articles hoping to thoroughly explain the basics of backbone.

With the introduction of the previous series of articles, we'll go into the important stuff about the use of Backbone.model, Backbone.collection, Backbone.view, and Backbone.sync functions.



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.