此代码返回模型在集合中出现的次数的警报消息。我想只打印一次,并在用户名和密码匹配后立即退出循环。 该怎么办?
this.collection.find(function(model)
{
debugger
var user = model.get('username');
var pwd = model.get('password');
if(enteredUsername == user && enteredPassword == pwd)
{
return(alert("success"));
}
else
{
return(alert("failure"));
}
});
最佳答案
BB的来源
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;
});
},
¶
Return the first model with matching attributes. Useful for simple cases of find.
findWhere: function(attrs) {
return this.where(attrs, true);
},
所以尝试一下
console.log(this.collection.findWhere({username: username, password: password}));
关于javascript - Backbone Collection .findwhere() 下划线方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27958837/