ChatGPT解决这个技术问题 Extra ChatGPT

Accessing parent class in Backbone

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.
    },
});

Y
Yury Tarabanko

Try

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

this appears to work even in the presence of _.bindAll(this) calls, which render the __super__ property of the constructor undefined.
better than using __super__ has the underscores suggests a private member
R
Raynos
MyModel = BaseModel.extend({
    initialize: function() {
        MyModel.__super__.initialize.apply(this, arguments);
        // Continue doing specific stuff for this child-class.
    },
});

@Industrial then it's doing something silly. Try this.__super__.initialize.apply(this, arguments);
__ super __ is not intended to use it directly, as the underscored name implies.
@Raynos: Why not BaseModel.prototype.initialize.apply(this, arguments);? this should work without __super__.
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...
FWIW Jeremy Ashkenas states that __super__ is not intended to be used directly.
r
rookieRailer

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.


w
wheresrhys

I think it'd be

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

It's this.__super__.initialize.call(this);
Both this.__super__.prototype.initialize.call(this); and this.__super__.initialize.call(this); throws this.__super__ is undefined in Firebug.
Try this.constructor.__super__.initialize.call(this);
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);
FWIW Jeremy Ashkenas states that __super__ is not intended to be used directly.
C
Community

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

Backbone.Model.prototype.initialize.call(this);

c
culurienneldoreth

Similar to @wheresrhys, but I would use apply instead of call in case BaseModel.initialize is expecting arguments. I try to avoid processing the attributes map that can be passed to a Backbone Model upon initialization, but if the BaseModel were actually a View or a Collection then I might want to set options.

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

FWIW Jeremy Ashkenas states that __super__ is not intended to be used directly.
B
Bnaya

here's a multi generation callSuper method, just add it to your extending class.

callSuper: function (methodName) {
    var previousSuperPrototype, fn, ret;

    if (this.currentSuperPrototype) {
        previousSuperPrototype = this.currentSuperPrototype;
        // Up we go
        this.currentSuperPrototype = this.currentSuperPrototype.constructor.__super__;
    } else {
        // First level, just to to the parent
        this.currentSuperPrototype = this.constructor.__super__;
        previousSuperPrototype = null;
    }

    fn = this.currentSuperPrototype[methodName];

    ret = (arguments.length > 1) ? fn.apply(this, Array.prototype.slice.call(arguments, 1)) : fn.call(this);

    this.currentSuperPrototype = previousSuperPrototype;

    return ret;
}

D
Dani Cricco

You might consider rewriting your code using functional inheritance.

var BackBone=function(){
    var that={};

    that.m1=function(){

   };
   return that;

};

var MyModel=function(){

 var that=BackBone();
 var original_m1=that.m1;

//overriding of m1
 that.m1=function(){
    //call original m1
 original_m1();
 //custom code for m1
  };
};

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.