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: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'sfetch
method.See http://japhr.blogspot.in/2011/10/overriding-url-and-fetch-in-backbonejs.html for more details.
If you would like to keep fetch "thenable" for promises then you could also do something like this:
Backbone collection has two methods to set new data add and reset. Let's say you want to replace all collection data with the incoming data and therefor use the reset:
If you need to do this for every model and/or collection, override
Backbone.ajax
.Overriding
Backbone.ajax
gives you the requestoptions
that would be normally passed to$.ajax
. You only need to return the response of$.ajax
(or some otherPromise
) and don't need to worry about setting stuff in the collection/model.I am using something like this:
The main point beeing I use
collection.reset(data)
to reinitialize the collection