I want to get my collection in a NON-RESTful way, so I decide to override the Collection.fetch
with
App.carClc = Backbone.Collection.extend({ model : App.cardModel, url : 'http://localhost/bbtest/data.php', fetch : function() { $.ajax({ type : 'GET', url : this.url, success : function(data) { console.log(data); } }); } });
I don't know how to set my collection to the response. I'm new to BackboneJS, thanks all of you!
If you want to add a custom "decorator" to fetch
, but not override it completely, try:
var MyCollection = Backbone.Collection.extend({ //custom methods fetch: function(options) { //do specific pre-processing //Call Backbone's fetch return Backbone.Collection.prototype.fetch.call(this, options); } });
Here, you don't have to roll out your own $.ajax
Also, don't forget the return
in the last line if you want to use the jQuery promise returned by Backbone's fetch
method.
See http://japhr.blogspot.in/2011/10/overriding-url-and-fetch-in-backbonejs.html for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With