Backbone findWhere什么都没发现

时间:2017-02-09 10:22:02

标签: javascript backbone.js

我的收藏有4494款。我尝试使用findWhere来获取id的特定模型。 findWhere返回'undefined'。

如果我限制模型的数量,它可以正常工作。当模型数量超过100时,这种陌生感就会出现。

var users = this.usersCollection;

console.log(users);
  

console screen

console.log(users.findWhere({uid: 1}));
  

collection screen

2 个答案:

答案 0 :(得分:0)

问题已解决(在实际提取集合之前使用findWhere )感谢TJ's comment,您可能希望使用.get代替{ {1}}按ID搜索模型时。

如果您的findWhere模型看起来像这样:

User

您可以直接从该集合中通过ID获取用户:

var User = Backbone.Model.extend({
    idAttribute: 'uid',
    // ...
});

您还可以将var user = users.get(1); 与模型的get或模型实例一起使用。

cid

这是更好的,因为除了通常的数组外,Backbone还保留了具有指定id属性的模型的哈希值。

答案 1 :(得分:0)

问题是我在完全取出之前尝试使用我的收藏。

相关问题