javascript - How to fetch a specific attribute and put it in a collection in backbone.js? -


i fetch specific attribute , put in collection. json looks this:

{    foo: "lorem ipsum",    bars: [{        a: "a",        b: {c: "d", e: "f"}    }, {        a: "u",        b: {w: "x", y: "x"}    }] } 

i understand how fetch bars (and not foo bars) , returned somewhere using parse, fetch bars, identify object using attribute a , put b inside of collection.

my idea like

mycollection = backbone.collection.extend({     model: mymodel,     url: "someurl",      parse: function(data) {          data.bars.each(function(bar) {              if (bar.a == i) {                  return bar.b;              }          }     } };  var mycollection = new mycollection(); mycollection.fetch({      success: function() {           console.log("proper collection of correct 'b'!");      } }); 

i having trouble knowing , how pass i if(bar.a == i).

the options pass fetch forwarded collection.parse , means can write this:

mycollection = backbone.collection.extend({     model: mymodel,     url: "someurl",      parse: function(data, opts) {         console.log(opts);          // took liberty of replacing filter         var matches = _.where(data.bars, {a: opts.i});         return _.map(matches, "b");     } });  var mycollection = new mycollection();  // pass filter option when fetch collection mycollection.fetch({     i: "u",      success: function() {         console.log("proper collection of correct 'b'!");     } }).then(function() {     console.log(mycollection.tojson());         }); 

and demo http://jsfiddle.net/nikoshr/v57rpgu9/


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -