populating a collection from a json file
I want to populate a collection from an json file, which has other data apart from the array of data items. I came to know that parse function on collection should be used to return the array of data items, but my collection is not getting populated from the json file. When i kept a breakpoint at parse method in firebug, the control is not at all coming there.
This is my code
var m1 = Backbone.Model.extend({
title:'Title1',
tag:'html',
date: 'Today'
})
cll = Backbone.Collection.extend({
url:'/combodata.json?uu',
model:m1,
parse:function(res){
return res.items;
}
});
ci = new cll();
ci.fetch();
The json response will be like this
{
'identifier': 'title',
items:[
{title:'A', tag:"htmlcss", date:'today'},
{title:'AA', tag:"htmlcss", date:'today'},
{titl开发者_如何转开发e:'B', tag:"htmlcss", date:'today'},
{title:'C', tag:"htmlcss1", date:'today'}
]}
Please point me where I'm wrong.
Not an expert at JSON but I tried to validate your JSON at jsonlint.com. It fails at line 2
Parse error on line 1:
{ 'identifier': 'title
-----^
Expecting 'STRING', '}'
Kindly verify if your JSON is valid or not.
Just checked another site for your JSON validation http://jsonformatter.curiousconcept.com/ and it seems, that string identifiers should be used with double quotes instead of single quotes. Replacing your single quotes with double quotes might help.
精彩评论