Backbone.js super() function

joshontheweb

Josh Nielsen

2 years ago · 5,670 views
Backbone.Model.prototype._super = function(funcName){
    return this.constructor.__super__[funcName].apply(this, _.rest(arguments));
}

// used like this
Model = Backbone.model.extend({
    set: function(arg){
        // your code here

        // call the super class function
        this._super('set', arg);
    }
});

// It is kind of a bummer that you still have to specify the functions name, 
// but I like it better than the alternatives.

// using the constructor with out the super wrapper:
    this.constructor.__super__.set.apply(this, args)
// or referencing the object directly:
    Backbone.Model.prototype.set.apply(this, args);
Raw

This adds a super function to the Backbone.Model prototype that allows you to call the a function on the super class of a model.

Tagged: backbone backbone.js frameworks javascript
Comments are only visible to Forrst members. Log in or Request an invite.
3 new notifications