2013-02-13 62 views
0

我正在使用集合url进行服务调用。该服务返回一些JSON,JSON长度为13(简而言之,13行数据)。... fetch()和toJSON()响应不匹配

这里,this.Collection.fetch()将返回JSON谁的长度是13 但this.Collection.toJSON()将返回JSON谁的长度是12 相反,它应该返回长度13

在集合解析中,响应返回长度为13的json,这是正确的!

tableTemplate是模板的对象(模板使用Handlebars.js完成)。

this.Collection.fetch({ 成功:函数(){

 console.log("Collection Fetch 2:"); 
     console.log(this.Collection.fetch()); 

     console.log("Collection toJSON: "); 
     console.log(this.Collection.toJSON()); 
     console.log(this.Collection.toJSON().length); 


     var markup = tableTemplate({List:self.importCollection.toJSON()}); 
     ... 
      ... 
    } 
}); 
+0

一些更多的上下文会有所帮助 – JamesHalsall 2013-02-13 12:31:44

+1

目前还不清楚在同一个集合的'fetch'回调中调用'fetch'时你期望的工作。第二个'fetch'也是异步的。 – WiredPrairie 2013-02-13 12:57:25

+0

你能否再次审查它,并解释我在我的代码中发生了什么。 – 2013-02-13 13:17:39

回答

1

那的JavaScript /骨干网的异步性

做这样的:

this.collection.fetch(); 

//Fetch() is asynchronous call , when it completes fetching it triggers the **reset** event so you need a event listener for **reset** 

this.collection.on('reset',function(data){ 
console.log(data);       // this will log the object 
}); 

this.collection.on('reset',function(data){ 
console.log(JSON.stringify(data));  // This will log the JSON data 
}); 

OR你可以这样做:

this.collection.fetch({ 
success : function(){      // called on completion of fetch() 
console.log(data);  
console.log(JSON.stringify(data)); 
} 
}); 
+0

是的,但在“数据”响应有12个模型,而应该有13个模型! – 2013-02-14 05:38:09

+0

this.collection.length给你什么? – 2013-02-14 06:03:53

+0

这是给我12,而应该是13. – 2013-02-14 08:12:39