1
votes

I'm trying to set up a model using backbone that loads from a remote url: https://api.github.com/legacy/repos/search/javascript. Here is what I have so far.

var Repo= Backbone.Model.extend({});

var RepoCollection = Backbone.Collection.extend({
    url : "https://api.github.com/legacy/repos/search/javascript",
    model : Repo
});


var repos = new RepoCollection();

repos.fetch({success: function(){
    console.log(repos.models);
}});

This just gives me an empty array. Why does this not work? This url just contains a non-empty JSON array. I've also tried the parse function without any success.

parse : function(data) {
   return data.results;
}

If the github api does not support this kind of call, does anyone have an example of a remote url where I can use backbone to fetch data?

Edit: I should add that I looked at the network console on Chrome and I am getting a 200 OK response with correct JSON response from github. I guess I'm just having trouble figuring out how to access that data and populate my RepoCollection with it.

2

2 Answers

0
votes

You can try:

repos.fetch({success: function(data){
    console.log(data);
}});
0
votes

Your data is wrapped in a repositories key, not in results and looks like this

{
    "repositories": [
        ...
    ]
}

Try

var RepoCollection = Backbone.Collection.extend({
    url : "https://api.github.com/legacy/repos/search/javascript",
    model : Repo,

    parse : function(data) {
        return data.repositories;
    }
});

and a demo http://jsfiddle.net/nikoshr/vHX7C/