一个简单的下拉列表的 Backbone.js 视图

4
我已经构建了一个基于Backbone的库,允许用户添加/删除项目,就像Todos示例一样。
每次添加或删除项目 - 或者整个集合被刷新 - 我需要两个其他选择元素在页面的其他区域中重新填充最新的项目作为选项。这应该如何实现?我只需在持有对集合的引用的视图的渲染函数中重新填充选择元素吗?
我很想为选择选项创建一个视图,但这似乎有些过度设计,尤其是考虑到该视图不需要对任何事件做出反应。选择选项由其他视图用于填充表单数据。

这个库在哪里?我真的需要它! - George R
@GeorgeR http://documentcloud.github.com/backbone/ - UpTheCreek
2个回答

7
你是正确的:为每个选择选项创建一个独特的视图。这不是过度设计;这就是视图的作用。它们监听其模型(在本例中为项目列表)的事件,并在接收到事件后重新绘制自己。它们有容器指定,所以一旦你在View子类的参数中建立了这些指定,你就不需要再考虑它们了。你可以独立地对它们进行样式设置。
这就是视图的整个重点。
更重要的是,你还可以将“列表视图”抽象出来,然后每个特定的视图都可以从该视图继承,并添加两个功能:过滤器(“最新”)和渲染器。你必须编写渲染器;你可能会利用一些语法糖,使清楚地了解你正在渲染的内容。这比写注释要好。

0

不要在模板中循环集合。如果你想这样做,最好使用HTML片段和AJAX。相反,使用一个渲染其自身视图的Backbone View(细粒度是减少服务器同步和页面刷新的关键)。这是递归的,可以重复此模式,直到没有更多相关数据可循环。

——Jonathan Otto 在 《面向普通人的 Backbone.js 概念理解》 中指出

当你想渲染子视图时,当然会有一些注意事项

我进行了代码搜索,试图找到一些如何做这个的示例。结果发现TodoMVC示例是一个很好的模型。从Strider-CD源代码(MIT许可证)中得知:

var UserView = Backbone.View.extend({

    //... is a class. not sure how to put that here
    tagName: "option",

    // Cache the template function for a single item.
    template: _.template($('#user-item-template').html()),

    // The DOM events specific to an item.
    // maybe could put links here? but then user couldn't see on mouse-over

    // The UserView listens for changes to its model, re-rendering. Since there's
    // a one-to-one correspondence between a **User** and a **UserView** in this
    // app, we set a direct reference on the model for convenience.
    initialize: function() {
      _.bindAll(this, 'render');
      this.model.bind('change', this.render);
      this.model.bind('destroy', this.remove);
    },

    // Re-render the contents of the User item.
    render: function() {
      $(this.el).html(this.template(this.model.toJSON()));
      return this;
    },

    // Remove the item, destroy the model.
    clear: function() {
      this.model.clear();
    }

  });

  // The Application
  // ---------------

  // Our overall **AppView** is the top-level piece of UI.
  var UsersView = Backbone.View.extend({
    // Instead of generating a new element, bind to the existing skeleton of
    // the App already present in the HTML.
    el: $("#user-form"),

    // no events here either at this time

    // At initialization we kick things off by
    // loading list of Users from the db
    initialize: function() {
      _.bindAll(this, 'addAll', 'addOne','render');

      Users.bind('add', this.addOne);
      Users.bind('reset', this.addAll);
      Users.bind('all', this.render);

      console.log("fetching Users");
      Users.fetch();
    },

    // Re-rendering the App just means refreshing the statistics -- the rest
    // of the app doesn't change.
    render: function() {
      console.log("rendering User AppView");
      // might want to put some total stats for the Users somewhere on the page

    },

    // Add a single todo item to the list by creating a view for it, and
    // appending its element to the `<ul>`.
    addOne: function(User) {
      console.log("adding one User: " + User.get("id") + "/" + User.get("email"));
      var view = new UserView({model: User});
      this.$("#user-list").append(view.render().el);
    },

    // Add all items in the **Users** collection at once.
    addAll: function() {
      console.log("adding all Users");
      console.log(Users.length + " Users");
      Users.each(this.addOne);
    }


  });
  // Finally, we kick things off by creating the **App**.
  console.log("starting userapp now");
  var UsersApp = new UsersView();

});

选择列表视图与选项子视图的其他示例可在以下位置找到:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接