无法传递查询结果以查看和呈现为JSON

时间:2015-05-09 08:07:36

标签: javascript json parse-platform handlebars.js

我正在尝试查询Parse表,我可以将数据输出。然后我将该数据传递给Parse View以呈现为车把模板。当我手动将JSON数据输入到集合变量中时,视图本身就起作用了。

我的问题是数据没有从查询传递到View或者没有转换为JSON。我一直收到以下错误:

  

未捕获的TypeError:this.collection.toJSON不是函数

// 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);
} 
});  

我做错了什么?

1 个答案:

答案 0 :(得分:1)

可能是你正在编码,但没有解码?另外,我会检查this.collection的类型,尤其是this。由于Backbone.toJSON()适用于模型。

  

离。使用JSON.stringify()进行编码以将对象转换为json字符串以存储在db中,并在使用JSON.parse()从数据库检索后进行解码

Backbone uses .toJSON()this post explains进一步关于骨干

中的序列化/反序列化