[英]Handlebars won't loop over my Backbone.js Collection
我有一個Backbone應用程序,正在嘗試使用JSON文件填充集合。 我想從JSON生成“標題”列表,以最終變成菜單。 一切進行得很好,除了Handlebars不會在我的收藏中循環(每個)來呈現列表。
相關觀點:
var MenuView = Backbone.View.extend({
template: Handlebars.compile(
'<ul>' +
'{{#each items.models}}<li>{{attributes.title}}</li>{{/each}}' +
'</ul>'
),
initialize: function () {
this.listenTo(this.collection, "reset", this.render);
},
render: function () {
this.$el.html(this.template(items));
return this;
}
});
模型和集合:
var Magazine = Backbone.Model.extend({
urlRoot:"/items",
defaults: {
id: '',
title: '',
pubDate: '1/1',
image: ''
}
});
var MagazineMenu= Backbone.Collection.extend({
comparator: 'title',
model: Magazine,
url: "/items"
});
路由器:
var MagazineRouter = Backbone.Router.extend({
routes: {
"" : "listPage",
"titles/:id" : "showTitle"
},
initialize: function () {
this.magazineModel = new Magazine();
this.magazineModel.fetch();
this.magazineView = new MagazineView({
model: this.magazineModel
});
this.magazineCollection = new MagazineMenu();
this.magazineCollection.fetch();
this.menuView = new MenuView({collection: this.magazineCollection});
},
showTitle: function(id) {
this.magazineModel.set("id", id);
$("#theList").html(this.magazineView.render().el);
},
listPage : function() {
$('#theList').html(this.menuView.render().el);
}
});
var router = new MagazineRouter();
$(document).ready(function() {
Backbone.history.start();
});
最后是JSON:
[
{
"id": "screamingzebras",
"url": "screamingzebras",
"title": "Screaming Zebras",
"pubDate": "2/1",
"image": "screamingzebras.jpg"
},
{
"id": "carousellovers",
"url": "carousellovers",
"title": "Carousel Lovers",
"pubDate": "3/1",
"image": "carousellovers.jpg"
},
{
"id": "gardenstatuary",
"url": "gardenstatuary",
"title": "Garden Statuary",
"pubDate": "4/1",
"image": "gardenstatuary.jpg"
},
{
"id": "sombreromonthly",
"url": "sombreromonthly",
"title": "Sombrero Monthly",
"pubDate": "1/1",
"image": "sombreromonthly.jpg"
}
]
當我在瀏覽器中運行此程序時,在控制台中沒有任何錯誤。 如果我剛在調用this.$el.html(this.template(items));
之前console.log(this.collection)
this.$el.html(this.template(items));
在視圖中,我可以看到具有models屬性的集合,該集合已從JSON正確填充。 當我查看Chrome開發人員工具中的Elements面板時,可以看到它正在生成直至<ul>
標記的所有內容。 這使我相信,我只是缺少一個關鍵的邏輯要點,該邏輯要點是使Handlebars每個函數實際遍歷集合。
我在這里看到兩個問題:
items
未在任何地方定義,因此您的render
實際上是在說this.template(undefined)
。 items
的局部變量,您的Handlebars模板也不會知道您已將其稱為items
因此它不會知道{{#each items.models}}
應該對其進行迭代。 大概您的items
實際上應該是視圖的this.collection
並且render
應該看起來像這樣:
render: function () {
this.$el.html(this.template(this.collection));
return this;
}
那應該解決問題1 。 您可以通過兩種方式解決問題2 :
this.template
使items
與正確的事物相關聯。 第一個選項將使用上面的render
和如下所示的模板:
<ul>
{{#each models}}
<li>{{attributes.title}}</li>
{{/each}}
</ul>
第二個選項將保留您的模板,但更改render
以使用:
this.$el.html(
this.template({
items: this.collection
})
);
另一個選擇是使用this.collection.toJSON()
向模板提供數據,然后render
將使用:
this.$el.html(
this.template({
items: this.collection.toJSON()
})
);
然后模板將是:
<ul>
{{#each items}}
<li>{{title}}</li>
{{/each}}
</ul>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.