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);
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, 2022Comments
-
Industrial almost 2 years
I need to call the
initialize
method of the parent class, from inside the inheritedMyModel
-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 over 12 yearsIt's
this.__super__.initialize.call(this);
-
Sander over 12 yearsi 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 over 12 yearsBoth
this.__super__.prototype.initialize.call(this);
andthis.__super__.initialize.call(this);
throwsthis.__super__ is undefined
in Firebug. -
Raynos over 12 years@Industrial then it's doing something silly. Try
this.__super__.initialize.apply(this, arguments);
-
Yury Tarabanko over 12 years
__ super __
is not intended to use it directly, as the underscored name implies. -
wheresrhys over 12 yearsTry this.constructor.__super__.initialize.call(this);
-
Raynos over 12 years@YuryTarabanko sure it is, it's just someone choose a
__super__
naming convention in theirinherits
utility function to avoid naming conflicts. It's exposed by backbone -
Yury Tarabanko over 12 years@Raynos: Why not
BaseModel.prototype.initialize.apply(this, arguments);
? this should work without__super__
. -
Raynos over 12 years@YuryTarabanko either works. The
__super__
solution is more elegant because it doesn't require changing when you renameBaseModel
. -
Yury Tarabanko over 12 yearsdocumentcloud.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 almost 12 yearsIn Ember.js <3 you just do
this._super()
. tada! it's magic. :) -
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 over 11 yearsthis appears to work even in the presence of
_.bindAll(this)
calls, which render the__super__
property of the constructor undefined. -
Khalid Dabjan almost 11 yearsI 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 almost 11 yearsFWIW Jeremy Ashkenas states that
__super__
is not intended to be used directly. -
MikeSchinkel almost 11 yearsFWIW Jeremy Ashkenas states that
__super__
is not intended to be used directly. -
MikeSchinkel almost 11 yearsFWIW Jeremy Ashkenas states that
__super__
is not intended to be used directly. -
Marcel Falliere almost 10 yearsbetter than using
__super__
has the underscores suggests a private member