Backbone Collection .findwhere()下划线方法

时间:2015-01-15 07:52:24

标签: javascript

此代码返回警告消息模型在集合中出现的次数。我只想打印一次,并在用户名和密码匹配时立即退出循环。 该怎么办?

                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"));

                     }
                });

1 个答案:

答案 0 :(得分:1)

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}));