Search code examples
javascriptbackbone.jsbackbone.js-collectionsbackbone-model

How to skip parse function call in backbone model while calling fetch


I have a backbone model which has custom parse function, while calling the fetch on this model sometimes I wanted to skip the parse function in certain scenarios. How can I do it. I tried the following option which did not work.

myModel.fetch({
            parse: false,
            success: _.bind(function(model, response) {}, this),
            error: _.bind(function(model, response) {}, this)
        });

My model code:

var MyModel = BaseModel.extend({
       initialize: function() {
       console.log('EventCloneModel in initialize()');
       _.extend(Backbone.Model.prototype, Backbone.Validation.mixin);
    },

    url: function() {
        var url = gc.apiUrl;
        var locale = "en_US"
        url += '&locale=' + locale;
        return url;
    },

     parse: function(response) {
          //some parsing logic goes here
          return response;
       },


    getValidations: function(){
      return this.validation;
    }

  });

  return MyModel;

});

Solution

  • Put the skip condition in your parse function. How you determine the skip condition is up to you.

    parse: function(response) {
          if(skipParse)
              return response;
          //parse the response here. If the code reaches this point, 
          //it means you want to parse it.
          return response;
       },