0

我尝试将此服务器:http ://cshosting.webfactional.com/api/v1/projects/?format=json 获取 到一个主干.js 集合。然后我尝试 console.log 它,但它不起作用。这对我很重要,请帮助。

新闻:我发现它与 JSONP 有关。将很高兴听到有关此的更多信息。谢谢。

简而言之,这是我的代码的一部分:

window.ProjectList = Backbone.Collection.extend({

    model: Project,

    url:"http://cshosting.webfactional.com/api/v1/projects",

    parse: function(response) {
         return response.objects;
  }


});

另一部分:

window.HomeView = Backbone.View.extend({

    initialize:function () {
        this.projectList = new ProjectList();
        this.projectList.fetch({success : function() {console.log(this.projectList);    }});


        this.homeListView = new HomeListView({model: this.projectList});
    }

});
4

2 回答 2

1

The this on the fetch callback is not going to refer to your HomeView instance. Try using another variable to ensure you are referencing the desired object.

initialize:function () {
    var self = this;
    this.projectList = new ProjectList();
    this.projectList.fetch({success : function() {console.log(self.projectList);    }});


    this.homeListView = new HomeListView({model: this.projectList});
}

If that doesn't solve the problem, please describe what happens. Use the webkit inspector's network tab to make sure the correct GET request and response are being called. Make sure your parse function is being called and the response object is what you expect.

于 2012-08-20T03:32:01.613 回答
0

看起来你想要做更多这样的事情:

window.Project = Backbone.Model.extend({
url:"http://cshosting.webfactional.com/api/v1/projects/?format=json" 

}); 

window.ProjectList = Backbone.Collection.extend({

model: Project,
url:"http://cshosting.webfactional.com/api/v1/projects/?format=json"

});

window.HomeView = Backbone.View.extend({

initialize:function () {
    _.bindAll(this, 'render');
},

render: function(){
    var self = this;
    console.log(self.collection);
    return this; 
}

});

var project = new Project();
var collection = new ProjectList();
collection.fetch({
        success: function(result_collection, resp) {
            var view = new HomeView ({  collection: result_collection });
            view.render();
        }
 });
于 2012-08-20T04:56:08.423 回答