Backbone: No data added to model on fetch()
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 comes 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();