Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
50
rated 0 times [  56] [ 6]  / answers: 1 / hits: 29213  / 13 Years ago, mon, january 16, 2012, 12:00:00

I'm attempting to load JSON (from php's json_encode) into a Backbone JS collection. I've simplified the problem to:



var myJSON = '[{ id:1,name:some name,description:hmmm}]';

var myCollection = new MyCollection(myJSON, { view: this });


And:



MyObject = Backbone.Model.extend({

id: null,
name: null,
description: null
});

MyCollection = Backbone.Collection.extend({
model: MyObject,
initialize: function (models,options) { }
});


Error:




Uncaught TypeError: Cannot use 'in' operator to search for 'id' in




Similar Issue: Backbone: fetch collection from server



My JSON certainly appears to be in the right format, am I missing something obvious? I have attempted using simply id: 1 as opposed to id with the same result.


More From » collections

 Answers
5

Your JSON is still in string format. Pass it to JSON.parse before assigning it:



var myJSON = JSON.parse('[{id:1,name:some name,description:hmmm}]');

[#88001] Friday, January 13, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sabinal

Total Points: 144
Total Questions: 112
Total Answers: 107

Location: Ghana
Member since Mon, Aug 22, 2022
2 Years ago
;