I'm making a twitter client for my own friends, and im making it using Backbone.js and a php backend using Twitter oAuth API.
So here's the scenario:
the problem is tweeter sometimes returns the same tweet twice (like RT's and stuff), and since that ID exists in backbone collection, it produces this error:
Uncaught Error: Can't add the same model to a set twice,119896811958833150
and exits the program. how can I control this situation? or is there a better way to do this?
window.Tweet = Backbone.Model.extend({});
window.Timeline = Backbone.Collection.extend({
model: Tweet,
url: function(){
var id = (window.lastId) ? window.lastId + "/?" + Math.floor(Math.random()*99999) : "";
return "/connect/timeline/" + id;
}
});
Thanks (and sorry for my English)
Well you certainly have a race condition. If you manage to send two requests and have a very slowly responding server you might get the same tweet twice. I'm not sure if that's the problem in your case but it certainly is an option.
Given this is the source of your problem, you have at least two options to solve it:
EDIT
I think you're problem is simply that url /timeline/SOME-ID-HERE
returns all the newest tweets until the given id inclusive. I've checked the contents of the requests that your applications sends. The first one /timeline
is long and ends with an id 119912841942802432
. And this is the only id that is returned in your second request.
What I don't understand is how does the ID in the address relate to ids of your tweets. The second request address is /timeline/119912841942802430
. On the other hand 119912841942802430
doesn't match anything in the results. 119912841942802432
(notice 2
instead of 0
at the end) does.