Home > Web Front-end > JS Tutorial > body text

JavaScript Backbone.js framework environment construction and Hello world example_Basic knowledge

WBOY
Release: 2016-05-16 15:01:37
Original
1790 people have browsed it

Environment preparation
Before learning Backbone formally, you need to prepare some things:
First, you need to get the Backbone framework source files: http://documentcloud.github.com/backbone/
Backbone relies on the basic methods of the Underscore framework, so you also need to download the source files of the Underscore framework: http://documentcloud.github.com/underscore/
In Backbone, operations on DOM and events rely on third-party libraries (such as jQuery or Zepto). You only need to choose one to download:
jQuery: http://jquery.com/
Zepto: http://zeptojs.com/
It seems quite troublesome, but the purpose of Backbone is to use a simple framework to build complex applications, so trouble does not mean that it is complicated.
You can create a new HTML page and introduce these frames, like this:

<script type="text/javascript" src="jquery/jquery-1.8.2.min.js"></script> 
<script type="text/javascript" src="underscore/underscore-min.js"></script> 
<script type="text/javascript" src="backbone/backbone-min.js"></script> 

Copy after login

At this point, you have prepared the environment necessary to run Backbone.

Hellow World
Let’s first talk about the functions of this helloworld: There is a report button on the page. Click the pop-up input box, enter the content, confirm, and finally the content will be added to the page. The page picture is as follows:

201657111923472.jpg (1366×768)

Look at the code below:

<!DOCTYPE html>
<html>
<head>
    <title>the5fire.com-backbone.js-Hello World</title>
</head>
<body>
<button id="check">报到</button>
<ul id="world-list">
</ul>
<a href="http://www.the5fire.com">更多教程</a>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone-min.js"></script>
<script>
(function ($) {
    World = Backbone.Model.extend({
        //创建一个World的对象,拥有name属性
        name: null
    });

    Worlds = Backbone.Collection.extend({
        //World对象的集合
        initialize: function (models, options) {
            this.bind("add", options.view.addOneWorld);
        }
    });

    AppView = Backbone.View.extend({
        el: $("body"),
        initialize: function () {
            //构造函数,实例化一个World集合类,并且以字典方式传入AppView的对象
            this.worlds = new Worlds(null, { view : this })
        },
        events: {
            "click #check": "checkIn",  //事件绑定,绑定Dom中id为check的元素
        },
        checkIn: function () {
            var world_name = prompt("请问,您是哪星人&#63;");
            if(world_name == "") world_name = '未知';
            var world = new World({ name: world_name });
            this.worlds.add(world);
        },
        addOneWorld: function(model) {
            $("#world-list").append("<li>这里是来自 <b>" + model.get('name') + "</b> 星球的问候:hello world!</li>");
        }
    });
    //实例化AppView
    var appview = new AppView;
})(jQuery);
</script>
</html>

Copy after login

I think the code is intuitive. It involves the three parts of backbone, view, model, and collection, which will be mentioned later. As long as you understand here, model represents a data model, collection is a collection of models, and view It is used to process pages and simple page logic.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!