Although it isn't explicitly documented, Collection#where
uses strict equality (===
) when searching. From the fine source code:
where: function(attrs, first) {
if (_.isEmpty(attrs)) return first ? void 0 : [];
return this[first ? 'find' : 'filter'](function(model) {
for (var key in attrs) {
if (attrs[key] !== model.get(key)) return false;
}
return true;
});
},
note the attrs[key] !== model.get(key)
inside the callback function, that won't consider 10
(a probable id
value) and '10'
(a probable search value extracted from an <input>
) to be a match. That means that:
customers.where({musketeerId: 10});
might find something whereas:
customers.where({musketeerId: '10'});
won't.
You can get around this sort of thing with parseInt
:
// Way off where you extract values from the `<input>`...
options.id = parseInt($input.val(), 10);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…