下面的backbone.js示例中的模型、控制器、视图是什么?

发布于 2024-12-25 12:12:26 字数 1240 浏览 0 评论 0原文

我得到了这个代码:

(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);

来自这个教程。

据我了解,Backbone.js 向前端引入了 MVC 模式。 但在上面的代码中我看不到这一点。

谁能向我解释一下吗?

I got this code:

(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);

from this tutorial.

I understand that Backbone.js introduces a MVC pattern to the front end.
But in the code above I can't see that.

Can anyone explain that to me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羅雙樹 2025-01-01 12:12:26

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

您列出的链接 - http://arturadib.com/hello-backbonejs/ - 可能是最好的学习 Backbone.js 的方法 - 特别是在 Javascript 背景很少的情况下。所以你走在正确的轨道上。该项目是backbone.js ToDo 列表教程的直接引入: http:// documentcloud.github.com/backbone/docs/todos.html

这个网站还将在更基本的层面上解释一些事情 - 我发现它非常有帮助:http://backbonetutorials.com/

There is technically no controller in backbone.js. The main structures are Models, Views, Collections (that act as arrays and contain lots of models), and Routers.

The link you listed - http://arturadib.com/hello-backbonejs/ - is probably the best way to learn Backbone.js - especially with little background in Javascript. So you are on the right track. That project is a direct lead-in to the backbone.js ToDo list tutorial: http://documentcloud.github.com/backbone/docs/todos.html

This site will also explain things at a more basic level - I found it very helpful: http://backbonetutorials.com/

执手闯天涯 2025-01-01 12:12:26

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

That's just the view part code. See other .js files in the same tutorial. Better check out all the files from 1.js to 5.js
Better check it from first: Hello Backbone

月隐月明月朦胧 2025-01-01 12:12:26

请注意,Backbone View 并不是您所期望的 MVC 中的视图,它更像是 MVP 中的控制器或演示者。这是一篇很好的文章,描述了这种差异。

Note that Backbone View isn't the one you expected in MVC its more like a controller or the presenter in MVP. Here is a nice article that describes this differences.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文