Accessing parent class in Backbone

38,822

Solution 1

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

Solution 2

Try

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

Solution 3

This worked for me, when I was trying to inherit among my models:

MyModel.prototype.initialize.call(this, options);

Referenced from http://documentcloud.github.com/backbone/#Model-extend

Thanks.

Solution 4

I think it'd be

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

Solution 5

this seems to be almost a duplicate of Super in Backbone, so you want something like this:

Backbone.Model.prototype.initialize.call(this);
Share:
38,822
Industrial
Author by

Industrial

I just want to lie on the beach and eat hot dogs. That’s all I’ve ever wanted. Really.

Updated on July 05, 2022

Comments

  • Industrial
    Industrial almost 2 years

    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.
        },
    });
    
  • Raynos
    Raynos over 12 years
    It's this.__super__.initialize.call(this);
  • Sander
    Sander over 12 years
    i doubt rewriting backbone is in order here :) we got to manage with the structure backbone gives us, and there are far better solutions than rewriting all this, like the super is more than enough.
  • Industrial
    Industrial over 12 years
    Both this.__super__.prototype.initialize.call(this); and this.__super__.initialize.call(this); throws this.__super__ is undefined in Firebug.
  • Raynos
    Raynos over 12 years
    @Industrial then it's doing something silly. Try this.__super__.initialize.apply(this, arguments);
  • Yury Tarabanko
    Yury Tarabanko over 12 years
    __ super __ is not intended to use it directly, as the underscored name implies.
  • wheresrhys
    wheresrhys over 12 years
    Try this.constructor.__super__.initialize.call(this);
  • Raynos
    Raynos over 12 years
    @YuryTarabanko sure it is, it's just someone choose a __super__ naming convention in their inherits utility function to avoid naming conflicts. It's exposed by backbone
  • Yury Tarabanko
    Yury Tarabanko over 12 years
    @Raynos: Why not BaseModel.prototype.initialize.apply(this, arguments);? this should work without __super__.
  • Raynos
    Raynos over 12 years
    @YuryTarabanko either works. The __super__ solution is more elegant because it doesn't require changing when you rename BaseModel.
  • Yury Tarabanko
    Yury Tarabanko over 12 years
    documentcloud.github.com/backbone/#Model-extend Brief aside on super: JavaScript does not provide a simple way to call super — the function of the same name defined higher on the prototype chain. If you override a core function like set, or save, and you want to invoke the parent object's implementation, you'll have to explicitly call it...
  • Salman von Abbas
    Salman von Abbas almost 12 years
    In Ember.js <3 you just do this._super(). tada! it's magic. :)
  • Raynos
    Raynos almost 12 years
    @SalmanPK I'm sure that will break horribly in plenty of edgecases. It's impossible to implement a sensibly semantic super in JS.
  • Scott Weaver
    Scott Weaver over 11 years
    this appears to work even in the presence of _.bindAll(this) calls, which render the __super__ property of the constructor undefined.
  • Khalid Dabjan
    Khalid Dabjan almost 11 years
    I wanted to pass arguments to the initialize function, and this is the only version that worked for me this.constructor.__super__.initialize.call(this,arguments);
  • MikeSchinkel
    MikeSchinkel almost 11 years
    FWIW Jeremy Ashkenas states that __super__ is not intended to be used directly.
  • MikeSchinkel
    MikeSchinkel almost 11 years
    FWIW Jeremy Ashkenas states that __super__ is not intended to be used directly.
  • MikeSchinkel
    MikeSchinkel almost 11 years
    FWIW Jeremy Ashkenas states that __super__ is not intended to be used directly.
  • Marcel Falliere
    Marcel Falliere almost 10 years
    better than using __super__ has the underscores suggests a private member