简体   繁体   English

骨干网从ID获取模型

[英]Backbone get model from id

Below is a model in my collection, how can I extract it from the collection - finding it by the id? 以下是我的收藏夹中的一个模型,如何从收藏夹中提取它-通过ID查找?

I usually do it with: 我通常这样做:

var twitter_id = window.localStorage.getItem("twitter_id");
AttoriBackEnd.twitter.fetch({nome:nomecercato,type:'twitter'});
AttoreTwitter= AttoriBackEnd.twitter.get(twitter_id);

But now it doesn't extract anything (I'm using backbone 1.1.2, with another version it worked perfectly). 但是现在它什么也没提取(我使用的是主干1.1.2,另一个版本运行良好)。

models: Array[4]
 0: child
_changing: false
_events: Object
_pending: false
_previousAttributes: Object
attributes: Object
changed: Object
cid: "c50"
collection: child
id: "8696369"

Simply just use built-in method for collection (findWhere) 只需使用内置方法进行收集(findWhere)

To return single model that match twitter_id from collection 从集合中返回与twitter_id匹配的单个模型

AttoreTwitter = AttoriBackEnd.twitter.findWhere({
    // If model's id is twitter_id then
    id: twitter_id
    // else if model's twitter_id is set on model itself then
    twitter_id: twitter_id
});

Good Luck 祝好运

Based on what I see, I feel like you need to wait til fetch completes then you can get the certain model in the collection. 根据我所看到的,我觉得您需要等待直到fetch完成,然后才能在集合中获取特定的模型。 Here's one way to do it: 这是一种实现方法:

var twitter_id = window.localStorage.getItem("twitter_id");
AttoriBackEnd.twitter.fetch({nome:nomecercato,type:'twitter'});

var AttoreTwitter;
AttoriBackEnd.twitter.on('sync', function(){
  AttoreTwitter= AttoriBackEnd.twitter.get(twitter_id);
  console.log(AttoreTwitter); // Test to see
  // Continue code here
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM