2

我可以像这样创建一个简单的模型:

define(["models/base/model"], function(Model) {
  "use strict";

  var IssueModel = Model.extend({
    defaults:{
      lastName: "Bob",
      firstName: "Doe"
    }
  });
  return IssueModel;
});

然后从我的控制器我可以这样做:

this.model = new IssueModel();

然后当我创建我的视图时,我可以像这样传递我的模型:

this.view = new IssueView({model: this.model});

最后,在我的模板中,我可以通过执行以下操作成功获取模型的属性:

Hi {{firstName}} {{lastName}}

但是,当我使用定义集合IssueModel并尝试将集合传递给我的视图(而不是像我之前展示的模型)时,我无法弄清楚如何在我的 Handlebars 模板中引用模型:

var self = this;
this.collection = new IssueCollection();
this.collection.fetch({
  success: function(collection) {
    self.view = new IssueView({collection: collection});
    console.log(self.collection);
  },
  error: function(collection, error) {
    // The collection could not be retrieved.
  }
});

我知道fetch从我的 Parse.com 后端正确检索了 5 个模型,因为这是我在控制台上得到的:

用于收集的控制台输出

我的问题是这个。我知道 Chaplin.js 使用getTemplateData,但是当我传递一个模型时,我不需要做任何特别的事情来引用我视图中的属性。我将如何引用,特别是迭代我在 Handlebars 模板中传递给我的视图的集合?

{{#each [Model in the collection I passed to the view]}}
  {{title}}
{{/each}}
4

1 回答 1

1

卓别林将使用CollectionView呈现集合,它基本上是普通视图的扩展,它监听集合中的变化并相应地添加/删除子视图。

this.view = new IssueCollectionView({collection: this.collection});

此外,使用集合视图时无需等待成功调用,因为它会在添加数据时自动呈现每个子项。

于 2013-10-23T12:12:42.937 回答