Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.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
Javascript Backbone.js集合获取,can';无法检索项目_Javascript_Rest_Backbone.js_Fetch_Backbone.js Collections - Fatal编程技术网

Javascript Backbone.js集合获取,can';无法检索项目

Javascript Backbone.js集合获取,can';无法检索项目,javascript,rest,backbone.js,fetch,backbone.js-collections,Javascript,Rest,Backbone.js,Fetch,Backbone.js Collections,我是Backbone.js的新手,我正在学习一个教程,试图使它适应我的需要。 我正在从主应用程序视图调用fetch方法,以便检索要插入到集合中的多个对象。 我可以在chrome中看到json数据被返回,fetch函数返回success,但集合没有填充 我正在使用IcanHaz进行渲染。它只打印出我在作业模型中定义的默认模型。 var Job = Backbone.Model.extend({ defaults: { title: 'Not specified',

我是Backbone.js的新手,我正在学习一个教程,试图使它适应我的需要。
我正在从主应用程序视图调用fetch方法,以便检索要插入到集合中的多个对象。 我可以在chrome中看到json数据被返回,fetch函数返回success,但集合没有填充

我正在使用IcanHaz进行渲染。它只打印出我在作业模型中定义的默认模型。

 var Job = Backbone.Model.extend({
   defaults: {
        title: 'Not specified',
        status: 0,
        created_at: 'Not specified'
    }
 });


var JobView = Backbone.View.extend({
   render: function () {
        // template with ICanHaz.js (ich)
        this.el = ich.jobRowTpl(this.model.toJSON());
        return this;
    }
});

// define the collection of jobs
var JobCollection = Backbone.Collection.extend({
    model: Job,
    url: '/api/1.0/jobs/'
});


// main app, the collection view
var AppView = Backbone.View.extend({
    tagName: 'tbody', 

    initialize: function() {
        this.jobs = new JobCollection();
        this.jobs.on('all', this.render, this);
        this.jobs.fetch({
                           error: function () {
                             console.log("error!!"); 
                           },
                           success: function () {
                              console.log("no error"); 
                           }
                        }).complete(function () {
                            console.log('done');
                            console.log('length1:' +  this.jobs.length);
                        });
       console.log('length2: '+ this.jobs.length);
    },

    render: function () {
        this.jobs.each(function (job) {
            var tmpjob = new JobView({model: job}).render().el;
            $(this.el).append(tmpjob);
            console.log('job': + this.tmpjob);
        }, this);

        return this;
    }


});

var app = new AppView();
$('#app').append(app.render().el);
在Chrome控制台中,我可以看到:

length2:0
job:undefined 
no error 
done 
Uncaught TypeError: Cannot read property 'length' of undefined //referring to the lenght1 logging
以下是我在network/xhr/response下从chrome inspector获取的数据:

{"count": 2, "next": null, "previous": null, "results": [{"id": 1, "title": "testAlbum", "status": 0, "created_at": "2012-12-31"}, {"id": 2, "title": "testAlbum2", "status": 0, "created_at": "2012-12-31"}]}

我不明白为什么在调用fetch方法后会存在“jobs”集合,但在调用“success”助手时,它在fetch块中是未定义的。

为什么尽管集合返回成功并且json数据是从服务器返回的,却没有填充它


我完全迷路了

在集合中添加一个
parse
方法,该方法只返回
结果
数组。集合需要是一个模型数组,而不是整个JSON响应


解释如何使用
parse

parse
方法添加到只返回
结果
数组的集合中。集合需要是一个模型数组,而不是整个JSON响应


解释如何使用
解析

谢谢,这很有效!有没有提示为什么我的集合对象的长度仍然等于零,以及为什么当我尝试从fetch方法中的success handler函数访问它时,它是未定义的?这仍然是我控制台的结果:
length2:0作业:未定义作业:未定义无错误完成未捕获类型错误:无法读取未定义的属性“length”
可能有几件事。第一,
这个
complete
回调中不会引用视图。您可以在变量中保存对
this
的引用。我需要对此进行检查,但我不确定当获取完成时集合将完全更新。最好只听集合上的
change
事件。您的
this
关键字是正确的。分配
var=this就足够了console.log('length1:'+that.jobs.length)。此外,length2日志记录等于0,因为所有js代码都将按顺序执行,但提取调用是异步的,因此当我尝试
console.log('length2:'+this.jobs.length)时集合仍然为空,因为提取函数尚未完成。这很有道理,谢谢你!谢谢,成功了!有没有提示为什么我的集合对象的长度仍然等于零,以及为什么当我尝试从fetch方法中的success handler函数访问它时,它是未定义的?这仍然是我控制台的结果:
length2:0作业:未定义作业:未定义无错误完成未捕获类型错误:无法读取未定义的属性“length”
可能有几件事。第一,
这个
complete
回调中不会引用视图。您可以在变量中保存对
this
的引用。我需要对此进行检查,但我不确定当获取完成时集合将完全更新。最好只听集合上的
change
事件。您的
this
关键字是正确的。分配
var=this就足够了console.log('length1:'+that.jobs.length)。此外,length2日志记录等于0,因为所有js代码都将按顺序执行,但提取调用是异步的,因此当我尝试
console.log('length2:'+this.jobs.length)时集合仍然为空,因为提取函数尚未完成。这很有道理,谢谢你!