Thursday, December 20, 2012

4 ways to call parent functions in backbone

Struggled with OOP concept in backbone.js when calling parent function, actually it is not backbone problem, but Javascript itself. After google, I found 4 most discussed ways to make sub class invoke parent class functions.

1. Use backbone plugin
https://github.com/lukasolson/Backbone-Super
this._super(attributes, options);

2. Use standard using Javascript style prototype
Backbone.Model.prototype.xxx.call(this)

3. Use the internal __super__ property
childModel.__super__.xxx.call(this)

4. Create _super function to Backbone Class
Backbone.Model.prototype._super = function(funcName){
    return this.constructor.__super__[funcName].apply(this, _.rest(arguments));
}
this._super('set', arg);

I suggest to use the plugin if possible.

1 comment: