Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding fetch() method in backbone model

Tags:

I would like to override the default fetch() method in a Backbone model, thus calling it only when needed.

Something like this:

Account.Check = Backbone.Model.extend({     model : Account.Item,      url : Settings.Url.checkAccount,      fetch : function(options) {                          if (someCondition()) {                     // do some stuff             } else {                super.fetch(options);                 }     } }); 

My question is how to provide the same behaviour as the default fetch() method in the // do some other stuff part?

like image 750
Drejc Avatar asked May 03 '12 07:05

Drejc


1 Answers

This should do it...

fetch : function(options) {                     if (someCondition()) {               // do some stuff            } else {               this.constructor.__super__.fetch.apply(this, arguments);               // Or (less flexible)               Backbone.Model.prototype.fetch.apply(this, arguments);                }          } 
like image 56
alex Avatar answered Sep 21 '22 07:09

alex