2015-05-09 55 views
0

我正在尝试查询分析表,并且能够获取数据。然后,我将这些数据传递给Parse视图,以便将其渲染到车把模板中。当我手动将JSON数据输入到集合变量中时,视图本身正在工作。无法传递查询结果以查看并呈现为JSON

我的问题是,数据没有从查询传递到视图或没有转换为JSON。我不断收到以下错误:

Uncaught TypeError: this.collection.toJSON is not a function

// render template with context data 
var StoresView = Parse.View.extend({ 
template: Handlebars.compile($('#storetable-tpl').html()), 
render: function(){ 
    var collection = { storeList: this.collection.toJSON() }; 
    this.$el.html(this.template(collection)); 
} 
}); 

// query the store table data 
allStores.equalTo("shape","Round"); 
allStores.limit(10); 
allStores.descending("updatedAt"); 
allStores.find({ 
success: function(results) { 
    var storesView = new StoresView({ collection: results }); 
    storesView.render(); 
    $('#stores-table').html(storesView.el); 
} 
}); 

我在做什么错?

+0

我只注意到,这些问题是不是从查询,查看传递数据。当我更改'code'var集合= {storeList:this.collection};然后我得到数组中的数据。 – Rahul

回答

1

难道你是编码,但不解码?另外,我会检查this.collection的类型,尤其是this。由于Backbone.toJSON()适用于型号。

ex. to encode using JSON.stringify() for converting objects to json string to store in db and to decode when after retrieving from db using JSON.parse()

Backbone uses .toJSON()this post explains进一步对序列化/ deserialzation骨干

+0

这真棒谢谢。我认为它不工作因为我试图使用该功能,而不包括backbone.js。你的链接帮助了,它现在使用这些解析和串联在一起。 var collection = {storeList:JSON.parse(JSON.stringify(this.collection))}; – Rahul

+0

太棒了!很高兴这有帮助。是。请记住在你的情况下编码和解码:)! – daxeh

+0

Upvote如果它帮助!由于它有价值的序列化干杯 – daxeh