0

I am trying to query a Parse table and I am able to get the data out. I am then passing that data to a Parse View to be rendered into a handlebar template. The view in itself is working when I manually input JSON data into the collection variable.

My problem is that the data is not getting passed to the View from the query or not getting converted to JSON. I keep getting the following error:

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

What am I doing wrong?

Etheryte
  • 24,589
  • 11
  • 71
  • 116
Rahul
  • 51
  • 3
  • I just noticed, the issues is not of passing the data from query to view. Coz when I change the 'code'var collection = { storeList: this.collection }; Then I get the data in an array. – Rahul May 09 '15 at 08:52

1 Answers1

1

Could it be that you are encoding, but not decoding? Also, I will check the type of this.collection, especially this. Since Backbone.toJSON() applies to models.

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() and this post explains further on serialization/deserialzation in backbone

Community
  • 1
  • 1
daxeh
  • 1,083
  • 8
  • 12
  • Thats awesome thanks. I think it wasnt working coz I was trying to use that function without including backbone.js. Your link helped out and it works now using these parse and stringify together. var collection = { storeList: JSON.parse(JSON.stringify(this.collection)) }; – Rahul May 09 '15 at 09:10
  • Great! Glad that helped. Yes. Remember to encode and decode :) in your case, both! – daxeh May 09 '15 at 09:33
  • Upvote if it helped! Since it had value in regards to serialization Cheers – daxeh May 09 '15 at 09:36
  • I wanted to but I do not have enough reputation points it seems. – Rahul May 09 '15 at 12:05