JavaScript學習2:通過helloworld來認識下backbone

來源:互聯網
上載者:User

先來說一下這個helloworld的功能:
在頁面上有一個報道的按鈕,點擊彈出輸入框,輸入內容,確認,最後內容會加到頁面上。頁面圖如下:



下面來看代碼:

[html] <!DOCTYPE html> 
<html> 
<head> 
    <title>the5fire.net-backbone.js-Hello World</title> 
</head> 
<body> 
<button id="check">報到</button> 
<ul id="world-list"> 
     
</ul> 
<a href="http://www.the5fire.net">更多教程</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("請問,您是哪星人?"); 
            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> 
<!DOCTYPE html>
<html>
<head>
    <title>the5fire.net-backbone.js-Hello World</title>
</head>
<body>
<button id="check">報到</button>
<ul id="world-list">
   
</ul>
<a href="http://www.the5fire.net">更多教程</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("請問,您是哪星人?");
            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>

 

 


我認為代碼是直觀的,這裡面涉及到backbone的三個部分,view、model、collection,以後都會提到,這裡只要瞭解,model代表一個資料模型,collection是模型的一個集合,而view是用來處理頁面以及簡單的頁面邏輯的。

 

摘自 the_fire的技術部落格

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.