2012-01-06 40 views
1

我得到这个代码:以下backbone.js示例中的模型,控制器,视图是什么?

(function($){ 
    var ListView = Backbone.View.extend({ 
    el: $('body'), // el attaches to existing element 

events: Where DOM events are bound to View methods. Backbone doesn't have a separate controller to handle such bindings; it all happens in a View. 

    events: { 
     'click button#add': 'addItem' 
    }, 
    initialize: function(){ 
     _.bindAll(this, 'render', 'addItem'); // every function that uses 'this' as the current object should be in here 

     this.counter = 0; // total number of items added thus far 
     this.render(); 
    }, 

render() now introduces a button to add a new list item. 

    render: function(){ 
     $(this.el).append("<button id='add'>Add list item</button>"); 
     $(this.el).append("<ul></ul>"); 
    }, 

addItem(): Custom function called via click event above. 

    addItem: function(){ 
     this.counter++; 
     $('ul', this.el).append("<li>hello world"+this.counter+"</li>"); 
    } 
    }); 

    var listView = new ListView();  
})(jQuery); 

this教程。

我明白Backbone.js在前端引入了MVC模式。 但在上面的代码中,我看不到。

任何人都可以解释给我?

+0

不使用MVC,看到批评在网络编程中的操作 – 2012-01-06 20:11:04

回答

1

这只是视图部分代码。请参阅同一教程中的其他.js文件。更好地1.js检查所有文件5.js 更好地首先检查它:Hello Backbone

+0

我一直在怀疑。 – alexchenco 2012-01-06 11:36:14

1

注意,骨干查看是不是你在MVC预期一个其更像是一个控制器或在MVP的主持人。这是一个不错的article,描述了这种差异。

2

backbone.js中技术上没有控制器。主要结构是模型,视图,集合(用作数组并包含大量模型)和路由器。

您列出的链接 - http://arturadib.com/hello-backbonejs/ - 可能是学习Backbone.js的最佳方式 - 尤其是在JavaScript很少的情况下。所以你在正确的轨道上。该项目是直接导入到Backbone.js的待办列表教程:http://documentcloud.github.com/backbone/docs/todos.html

这个网站也将在更基本的层面解释的东西 - 我发现它非常有用:http://backbonetutorials.com/

+1

也结帐http://backboneboilerplate.com – 2012-04-23 09:03:23

相关问题