override Backbone's Collection-fetch

backbone.js, javascript, web

Solution

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.

Problem

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!

Original source