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.

demo: http://jsfiddle.net/ambiguous/pzzv4/1/


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -