主干:将集合标题和集合项放入一个json文件中
我的网站上有一个常见问题解答。 问题在一个json文件中,但我想在json文件中也包含一些额外的信息(比如faq的标题)。 我的json文件如下所示:主干:将集合标题和集合项放入一个json文件中,json,backbone.js,Json,Backbone.js,我的网站上有一个常见问题解答。 问题在一个json文件中,但我想在json文件中也包含一些额外的信息(比如faq的标题)。 我的json文件如下所示: { "titel": "FAQ title", "items": [ { "question": "Question 1", "answer": "Answer 1" }, { "question": "Ques
{
"titel": "FAQ title",
"items": [
{
"question": "Question 1",
"answer": "Answer 1"
},
{
"question": "Question 2",
"answer": "Answer 2"
}
]
}
集合扩展代码:
Faq.Collection = Backbone.Collection.extend({
model: Faq.Model,
url: '/scripts/json/faq.json',
parse: function(response){
return response.items;
}
});
将解析这些项,因为这是用于渲染循环的。
但是我怎样才能在页面上显示标题呢
这是渲染函数:
render: function() {
this.el.innerHTML = this.template({
titel: 'Helpdesk'
});
console.log(this);
this.collection.each(function( faqitem ) {
var faqItemView = new Faq.Views.ModelView({ model: faqitem });
this.$el.find('.faq').append( faqItemView.render().el );
}, this);
return this;
}
我想把json文件的标题放在“Helpdesk”所在的位置
我希望我足够清楚首先更改您的解析函数,如下所示:
parse: function(response){
this.title = response.title;
return response.items;
}
然后在渲染函数中:
this.el.innerHTML = this.template({
titel: this.collection.title // pay attention to the titel not being title :)
});