【问题标题】:extracting new models form collections从集合中提取新模型
【发布时间】:2015-02-06 20:22:06
【问题描述】:

我正在尝试在社交提要上开发刷新功能,每分钟我都会检查是否有一些新推文。

我正在获取主干集合 (col1),1 分钟后我正在获取相同的集合 (col2) 以比较和提取新推文。

  @tweets = App.request "tweets:entities"
  that = @
  setInterval (->
    that.freshTweetsCol = App.request "tweets:entities"
    App.execute "when:fetched", that.freshTweetsCol, =>
      console.log(that.freshTweetsCol == that.tweets) 
      freshTweets = _(that.freshTweetsCol.models).without(that.tweets.models)
      freshTweets2 =  _.difference(that.freshTweetsCol.toJSON(),that.tweets.toJSON())
      console.log(freshTweets)
      console.log(freshTweets2)
    return
  ), 10000
  1. @tweets 给我收集了 20 个模型
  2. that.freshTweetsCol 为我提供了与 20 个模型相同的集合 在@tweets 中,如果没有新推文。
  3. console.log(that.freshTweetsCol == that.tweets) 给出错误,即使模型相同
  4. console.log(freshTweets) 为我提供了 20 个模型的数组,每个模型都称为“Tweet”
  5. console.log(freshTweets2) 给了我 20 个对象的数组

我怎样才能只检查新推文?为什么我不能使用 _.difference 方法来比较集合?

【问题讨论】:

    标签: backbone.js collections coffeescript marionette


    【解决方案1】:

    console.log(that.freshTweetsCol == that.tweets) 给出错误,即使模型相同

    是的,这发生了check this

    使用下划线与它进行递归比较的对象进行比较

    _.isEqual(obj1, obj2);
    

    difference 返回不是模型的对象数组,因此您应该实例化新的 Backbone 集合,例如

    frshTwe =  _.difference(that.freshTweetsCol.toJSON(),that.tweets.toJSON())
    var freshTweets = new TweetsCollection(freshTweets2 );
    

    或者您可以重置集合 (check this)

    【讨论】:

    • 奇怪的是 console.log(_.isEqual(freshTweetsCol.toJSON(),@tweets.toJSON())) 给出了真实和新鲜的Tweets1 = _.difference(freshTweetsCol,@tweets) freshTweets = new Backbone.Collection(freshTweets1) 提供 20 个模型的集合
    猜你喜欢
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 2016-12-11
    相关资源
    最近更新 更多