javascript - BackboneJS - get specific value from collection using _.max method -
i want specific value model inside collection. collection sorted id (contest_id) served database , gets delivered json. so, json looks like:
data : [{ "contest_id" : "3", "artist" : { "artist_name": "some name", "artist_cover" : "some image.jpg" } }, "contest_id" : "1", .... }]
now, have glued together:
var contestimage = _.max(this.collection.tojson(), function(cnt){ return cnt.contest_id; });
i highest contest_id, want how proceed when want grab image? , display it?
thanks in advance...
the max
method should available directly backbone collection (although it's underscore method, it's mixed backbone's collections), means can simplify code bit.
something should trick:
var model = this.collection.max(function (cnt) { return cnt.contest_id; }); var contest_id = model.get('contest_id');
the first section returns model want, , second gets contest id attribute.
to artist cover attribute, can convert json:
var artist_cover = model.tojson().artist.artist_cover;
or artist attribute model:
var artist_cover = model.get('artist').artist_cover;
Comments
Post a Comment