Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override Backbone's Collection-fetch

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!

like image 523
Daniel.Woo Avatar asked Nov 13 '12 09:11

Daniel.Woo


1 Answers

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.

like image 111
Pramod Avatar answered Sep 28 '22 21:09

Pramod