Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
主干:将集合标题和集合项放入一个json文件中_Json_Backbone.js - Fatal编程技术网

主干:将集合标题和集合项放入一个json文件中

主干:将集合标题和集合项放入一个json文件中,json,backbone.js,Json,Backbone.js,我的网站上有一个常见问题解答。 问题在一个json文件中,但我想在json文件中也包含一些额外的信息(比如faq的标题)。 我的json文件如下所示: { "titel": "FAQ title", "items": [ { "question": "Question 1", "answer": "Answer 1" }, { "question": "Ques

我的网站上有一个常见问题解答。 问题在一个json文件中,但我想在json文件中也包含一些额外的信息(比如faq的标题)。 我的json文件如下所示:

{
    "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 :)
});