Chaplin.js如何处理将集合传递给视图?

时间:2013-10-21 02:57:38

标签: backbone.js handlebars.js chaplinjs

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

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个模型,因为这是我在控制台上得到的:

Console output for collection

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

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

1 个答案:

答案 0 :(得分:1)

Chaplin将使用CollectionView呈现集合,它基本上是普通视图的扩展,用于侦听集合中的更改并相应地添加/删除子视图。

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

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

相关问题