最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbone: No data added to model on fetch() - Stack Overflow

programmeradmin4浏览0评论

I have problems when i try to fetch data to my Backbone model from the server. You get a response in JSON from from the server which I think looks to be right formatted. Can you see anything wrong with it? It looks like:

[{"id":"1","name":"Fawad Hassan","email":"[email protected]"},{"id":"2","name":"Bill 
Gates","email":"[email protected]"},{"id":"3","name":"Steve 
Jobs","email":"[email protected]"},{"id":"4","name":"Naveed 
Ahmad","email":"[email protected]"},{"id":"5","name":"Mr Zee","email":"[email protected]"}]

My code for the Backbone project looks like this, and I can't really find the problem there either.

window.AppModel = Backbone.Model.extend({
    url: function() {
        return '/ci/index.php/site/userpost';
    }
});

window.AppContr = Backbone.Collection.extend({
    model: AppModel,
    initialize: function() {
        this.model = new AppModel;
    }
});

window.App = new AppContr({name: "Markus"});

window.AppView = Backbone.View.extend({
    el: $("#content"),
    initialize: function() {
        this.render();
    },
    render: function() {
        console.log(App.model.toJSON());
    }
});

App.model.fetch();
window.View = new AppView;

I have problems when i try to fetch data to my Backbone model from the server. You get a response in JSON from from the server which I think looks to be right formatted. Can you see anything wrong with it? It looks like:

[{"id":"1","name":"Fawad Hassan","email":"[email protected]"},{"id":"2","name":"Bill 
Gates","email":"[email protected]"},{"id":"3","name":"Steve 
Jobs","email":"[email protected]"},{"id":"4","name":"Naveed 
Ahmad","email":"[email protected]"},{"id":"5","name":"Mr Zee","email":"[email protected]"}]

My code for the Backbone project looks like this, and I can't really find the problem there either.

window.AppModel = Backbone.Model.extend({
    url: function() {
        return 'http://dev.local/ci/index.php/site/userpost';
    }
});

window.AppContr = Backbone.Collection.extend({
    model: AppModel,
    initialize: function() {
        this.model = new AppModel;
    }
});

window.App = new AppContr({name: "Markus"});

window.AppView = Backbone.View.extend({
    el: $("#content"),
    initialize: function() {
        this.render();
    },
    render: function() {
        console.log(App.model.toJSON());
    }
});

App.model.fetch();
window.View = new AppView;
Share Improve this question edited Sep 9, 2011 at 8:11 Sai Kalyan Kumar Akshinthala 11.8k8 gold badges45 silver badges68 bronze badges asked Sep 9, 2011 at 6:52 Markus JönssonMarkus Jönsson 6551 gold badge6 silver badges7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You are doing a fetch on a Model, but returning Collection in response. That is main problem.

Second problem is that you are calling render on AppView totally random, i.e. it does not have anything to do with model or collection. Maybe there would be nothing in model when you render view. You should bind rendering to collection or model with bind. Than whenever you call fetch your view will re-render, which is one of main benefits of Backbone :)

Here es the code :)

window.Person = Backbone.Model.extend({});

window.Addressbook = Backbone.Collection.extend({
    url: 'http://dev.local/ci/index.php/site/userpost',// declare url in collection
    model: Person
}

window.Addresses = new AddressBook();

window.AppView = Backbone.View.extend({
    el: $('#content'),
    initialize: function() {
        Addresses.bind('reset', this.render); // bind rendering to Addresses.fetch()
    },
    render: function(){
        console.log(Addresses.toJSON());
    }
});

window.appview = new AppView();
Addresses.fetch();
发布评论

评论列表(0)

  1. 暂无评论