0

I am using backbone.js, and trying to fetch some json from twitter, but it doesn't work, the collection's length is 0. Here's the code.

           
var Tweet = Backbone.Model.extend();

var Tweets = Backbone.Collection.extend({
    model: Tweet,
    url: 'http://api.twitter.com/1/favorites.json?screen_name=dhh',
    parse: function(response) {
        return response.results;
    }
});

var tweets = new Tweets();
tweets.bind('reset', function(tweets) {
   alert(tweets.length);
});
tweets.fetch();

Andy
  • 751
  • 1
  • 11
  • 21
  • possible duplicate of [Backbone outputing empty array for collection.models?](http://stackoverflow.com/questions/11954243/backbone-outputing-empty-array-for-collection-models) – Derick Bailey Oct 02 '12 at 00:48
  • but that one doesn't have anything to do with json – Andy Oct 02 '12 at 02:47
  • possible duplicate of [Unable to fetch data from Twitter RESTful API using Backbone.js Collection](http://stackoverflow.com/questions/6224839/unable-to-fetch-data-from-twitter-restful-api-using-backbone-js-collection) – Edward M Smith Oct 02 '12 at 03:05
  • but that solution doesn't work for me as well, i paste the code from the answer, and it doesn't work. I updated the code by using the advice from the other question, but this time it doesn't even trigger alert. – Andy Oct 02 '12 at 03:40
  • My linked example works for me: http://jsfiddle.net/edwardmsmith/pKVFX/1/ Perhaps the `favorites` API call is more picky about origins - Your code throws an `origin not allowed` error from twitter when run on jsFiddle and also from `file://`. Are you running it from the filesystem? Or localhost? Or from a real server? Are you getting an ajax error. Supernova's answer blow is correct as well - the response is just an array – Edward M Smith Oct 02 '12 at 14:31

2 Answers2

1

try

parse: function(response) {
  return response;
}

pointing my browser to api.twitter.com/1/favorites.json?screen_name=dhh i don't see a .results property, just an array with objects

supernova
  • 3,814
  • 3
  • 22
  • 37
0

The solution is to get rid of the parse function, which has the same effect as the other answer suggests, and add &callback=?

Andy
  • 751
  • 1
  • 11
  • 21