Keep in mind that fetch
is an asynchronous call to the server. To make your code work as you expected, create a success
function that Backbone.Collection
can call once it refreshes its contents from the server:
$('#fetch').click(function(){
coll.fetch({
succcess: function() {
console.log(coll.toJSON());
}
});
});
Obviously you app doesn't need to really call console.log
so a more real world example would use backbone's event binding like this:
coll.bind('refresh', someFunction);
coll.fetch();
With this approach, backbone will call someFunction
when the collection refreshes. This is really useful especially when someFunction
is the render
function of a view to which you want to bind your collection.
coll.bind('refresh', yourView.render);
coll.fetch();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…