Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone Collection produces error when receiving server response
    text
    copied!<p>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. </p> <p>So here's the scenario:</p> <ol> <li>user authenticates</li> <li>backbone app initializes and sends a first request: window.timeline.fetch();</li> <li>window.timeline is a backbone collection and its url is: /connect/timeline/</li> <li>server returns 10 recent tweets in json and window.timeline adds them using Tweet model, and saves last Tweet id in a variable.</li> <li>backbone view renders and shows them and triggers a timer</li> <li>Timer starts ticking and runs window.timeline.fetch({add: true}); every 10 seconds, and adds /ID/ add the end of the fetch URL, to tell twitter API return tweets since that ID </li> <li>since I passed the add option, when server returns the object, it triggers "add" event and I bind it to a method which adds every tweet and the top of the list and saves the last tweet id to use in next timer tick.</li> </ol> <p>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:</p> <p>Uncaught Error: Can't add the same model to a set twice,119896811958833150</p> <p>and exits the program. how can I control this situation? or is there a better way to do this?</p> <pre><code>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; } }); </code></pre> <p>Thanks (and sorry for my English)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload