0

I am trying to develop refresh feature on social feed, Evry minute i am checking if there are some new tweets.

I am fetching a backbone collection (col1) and after 1 min i am fetching the same collection (col2) to compare and extract new tweets.

  @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 gives me collection with 20 models
  2. that.freshTweetsCol gives me collection with the same 20 models as in @tweets, if there is no new tweets.
  3. console.log(that.freshTweetsCol == that.tweets) gives false, even if models are the same
  4. console.log(freshTweets) gives me array of 20 models, each called 'Tweet'
  5. console.log(freshTweets2) gives me array of 20 objects

how can i check only for new tweets? why I cannot use _.difference method to compare the collections?

lipenco
  • 1,358
  • 5
  • 16
  • 30

1 Answers1

1

console.log(that.freshTweetsCol == that.tweets) gives false, even if models are the same

yes this happens check this

Use underscore to compare to objects it does recursive comparison

_.isEqual(obj1, obj2);

difference returns array of objects not model's so you should instantiate new Backbone collection like

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

or you can reset the collection (check this)

Community
  • 1
  • 1
StateLess
  • 5,344
  • 3
  • 20
  • 29
  • weird is that console.log(_.isEqual(freshTweetsCol.toJSON(),@tweets.toJSON())) gives true and freshTweets1 = _.difference(freshTweetsCol,@tweets) freshTweets = new Backbone.Collection(freshTweets1) gives collection with 20 models – lipenco Dec 09 '14 at 07:10