javascript - How to extract model from backbone collection on the basis of a field which is array of JSON -
i have collection of models in model contains field data, array of json.
data [{x : 3, y:4}]
now want use where function of backbone collection :
var model= coll.where ({ data : data })
it gives no output. not sure whether doing right or missing. please guide me soluiotn
if @ how where
works:
where: function(attrs, first) { if (_.isempty(attrs)) return first ? void 0 : []; return this[first ? 'find' : 'filter'](function(model) { (var key in attrs) { if (attrs[key] !== model.get(key)) return false; } return true; }); }
you'll see scanning models , comparing attributes using !==
. when using !==
or ===
(or !=
or ==
matter) compare arrays, references compared, not content; example, false:
[1] === [1]
the result searching array using where
doesn't work well, where
meant shortcut searching simple scalar values.
if need search array, can use filter
directly , can use _.isequal
compare things:
var models = coll.filter(function(m) { return _.isequal(m.get('a'), data); });
if want 1 match, use find
instead of filter
.
Comments
Post a Comment