Backbone.js Getting Started Tutorial

Source: Internet
Author: User

Don't talk nonsense, straight into the chase.

What is Backbone.js?

Backbone.js provides a set of web development frameworks thatModelsFor Key-value binding and custom event handling,CollectionsProvides a rich set of APIs for enumeration functions, event handling through views, and existing application to interact through the restful JSON interface, which is based onJQueryAndUnderscoreA front-end JS frame.

Important concepts in the backbone
    • model: An abstraction based on realistic data, such as people

    • collection: For example a group of people

    • view: Display data in model and collection, render data to the page

    • router: Control the routing of the page

With backbone, you can use your data as model, through model you can create data , validate data , destroy it, or save it to the server. When an action on the interface causes a change in the properties of the model, the model triggers the event of the changes. The views that are used to display the model state receive a message that the model triggers a change, and then respond to it and re-render the new data to the interface. In a complete backbone application, you don't need to write the glue code to get the node from the DOM with a special ID, or to manually update the HTML page, because when the model changes, the views are simply self-renewing.

Feel it.

Let's take a look at the previous example: a simple time-out clock system.
The following code:

<! DOCTYPE html><html><head>  <script src="Https://code.jquery.com/jquery-1.11.3.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/ Underscore-min.js "></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/ Backbone-min.js "></script>  <meta charset="Utf-8">  <meta name="viewport" content="Width=device-width">  <title>JS Bin</title></head><body>  <button id="Check">Please click here to punch in</button>  <ul id="Person-list"></ul></body><script>( function($) {//New model constructor      varPeople=backbone.model.extend ({name:NULL,//NameCTimeNULL//Punch -in time});//New collection constructor      varPeoples=backbone.collection.extend ({initialize: function(models,options) { This. Bind ("Add", Options.view.addOnePerson);//add adds the model to the collection, where it calls the Addoneperson function defined in view}      });//New View constructorAppview=backbone.view.extend ({el:$ ("Body"), Initialize: function() { This. peoples=NewPeoples (NULL, {view: This})//Instantiation of a collection}, events:{"click #check":"CheckIn",//Bind the #check Click event and execute the Checkin function}, CheckIn: function() {varPerson_name=prompt ("What's your name?" ");if(person_name=="") {Person_name="Passers -by"; }varCtime=New Date(). GetHours () +":"+New Date(). getminutes () +":"+New Date(). getseconds ();varperson=NewPeople ({name:person_name,ctime:ctime}); This. Peoples.add (person); }, Addoneperson: function(model) {          $("#person-list"). Append ("<li>"+model.get (' name ')+"Hello, your punch-in time is:"+model.get (' CTime ')+"</li>"); }    });varappview=NewAppview;//Instantiate a view to automatically execute functions in initialize}) (JQuery)</script></html>
Hands-on practice

This section explains the four sections of model, Collection, view, and router.

Model

For this part of the model, the official website says: "Model is the core of the JS application, including the underlying data and the logic around the data: Data transformation, validation, attribute calculation and access control." This statement essentially sums up the role of model in a project. In fact, not just JS applications, the model is an important piece of content in any project that collects and processes data.

On the web side, model also has an important function is to interact with the server side of the data, just as the server-side program needs to interact with the database. So the model should be something that carries data between the various modules.

So, Ajax operations should be written on the model layer.

( function() {varMan = Backbone.Model.extend ({defaults: {name:"Zhang San", Age:" the"}, Initialize: function() {//When initializing, the change event will occur prior to validate       This. Bind ("Change:name", function() {varName = This. Get ("Name"); Alert"You changed the Name property to:"+ name); }); This. Bind ("Invalid", function(model, error) {alert (error);    }); }, Validate: function(attributes) {if(Attributes.name = = ="') {return "name cannot be empty!" "; }}, Aboutme: function() {return "My Name"+ This. Get ("Name") +", this year"+ This. Get ("Age") +"Old"; }  });varMan =NewMan ();  Alert (Man.aboutme ()); Man.set ({name:"'}); Man.save ();//Verification})();

Model and server-side interaction, call the Save method to post all properties of the object to the server side, call fetch and send a GET request to the server side, the format of accepting data and sending data is in JSON format.

Next issue update collection.

Backbone.js Getting Started Tutorial

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.