Accessing parent class in Backbone

Industrial picture Industrial · Jan 23, 2012 · Viewed 38.3k times · Source

I need to call the initialize method of the parent class, from inside the inherited MyModel-class, instead of completely overwriting it as I am doing today.

How could I do this?

Here's what my code looks right now:

BaseModel = Backbone.Model.extend({
    initialize: function(attributes, options) {
        // Do parent stuff stuff
    }
});

MyModel = BaseModel.extend({
    initialize: function() {
        // Invoke BaseModel.initialize();
        // Continue doing specific stuff for this child-class.
    },
});

Answer

Yury Tarabanko picture Yury Tarabanko · Jan 23, 2012

Try

MyModel = BaseModel.extend({
    initialize: function() {
        BaseModel.prototype.initialize.apply(this, arguments);
        // Continue doing specific stuff for this child-class.
    },
});