简体   繁体   English

使用Backbone.js进行获取和解析的问题

[英]Problems with fetching and parsing using Backbone.js

I try to fetch this server: http://cshosting.webfactional.com/api/v1/projects/?format=json to a backbone.js collection. 我尝试获取此服务器: http : //cshosting.webfactional.com/api/v1/projects/?format=json到ribs.js集合。 Then I try to console.log it, but it's not working. 然后,我尝试console.log它,但是它不起作用。 It is very important to me, please help. 对我来说很重要,请帮忙。

NEWS: I figured out it's something with JSONP. 新闻:我发现这与JSONP有关。 will be glad to hear more information about that. 很高兴听到有关此的更多信息。 thanks. 谢谢。

this is parts of my code in short: 这是我的代码的一部分:

window.ProjectList = Backbone.Collection.extend({

    model: Project,

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

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


});

another part: 另一部分:

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});
    }

});

The this on the fetch callback is not going to refer to your HomeView instance. fetch回调上的this不会引用您的HomeView实例。 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. 使用webkit检查器的“网络”选项卡来确保调用了正确的GET请求和响应。 Make sure your parse function is being called and the response object is what you expect. 确保正在调用您的parse函数,并且response对象是您所期望的。

It seems like you would want to do something more like this: 似乎您想做更多这样的事情:

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();
        }
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM