Friday, 15 January 2010

parsing - Backbone How can i load only part of a json file into a collection -



parsing - Backbone How can i load only part of a json file into a collection -

hi have json file looks this:

{ "links_1": [ { "navn": "somename", "link": "http://www.domane.com" }, { "navn": "somename", "link": "http://www.domane.com" }, { "navn": "somename", "link": "http://www.domane.com" } ], "links_2": [ { "navn": "someothername", "link": "http://www.domane.com" }, { "navn": "someothername", "link": "http://www.domane.com" }, { "navn": "someothername", "link": "http://www.domane.com" } ] }

then have collection class. want create new collection contains links_1 array json file. , new collection contains links_2 array json file.

how accomplish that?

var col = new collection( collection.prototype.parse = function(response){ homecoming response.links_1; }

so illustrate further, want fetch specific json info objects.

var col = new collection(); col.fetch(links_1); // here want links_1 json info var col2 = new collection(); col2.fetch(links_2); // here want links_2 json info

any help great.

you're on right track trying override parse, you're doing incorrectly. try:

//define collection class var link1collection = backbone.collection.extend({ parse:function(response) { homecoming response.links_1 } }); //initialize new instance of collection class , fetch var collection = new link1collection(); collection.fetch();

edit: select field want extract on instance-to-instance basis, can pass custom alternative in fetch options. options passed parse:

//define collection class var linkcollection = backbone.collection.extend({ parse:function(response, options) { homecoming options.parsefield ? response[options.parsefield] : response; } }); //initialize new instance of collection class , fetch var collection = new linkcollection(); collection.fetch({parsefield:'links_1'});

json parsing backbone.js collections fetch

No comments:

Post a Comment