I am subclassing my own Backbone.View. If, in the super class' initialize function, I write:
_.bindAll(this, 'many', 'methods');
And specify the methods that I want to bind to this context, I can call super from the subclass via:
this.constructor.__super__.initialize.apply(this, arguments);
But, if in the super class, I use:
_.bindAll(this)
instead, when I go to call super from my subclass,
this.constructor.__super__
is undefined. Any wisdom on why that is?
I'm new to Backbone but have over 4 years experience with another framework that also expects you to pass an object containing methods to an extend method, but the problem is this does not give sufficiently fine-grained control to act upon the methods contained within the object being passed.
To gain more control you can instead pass a closure to extend, and the closure can return an object. This technique in general allows finer-grained control and more potential for sophistication, and can be leveraged to solve your specific issue.
Inside the closure that returns the methods, those methods can be split into two broad categories:
If we return the custom methods specific to our application from their own separate object, we can then use _.bind for "partial application" of _.bindAll to just those custom method names.
Putting it all together then:
Seeing as there's only solutions to this problem but not explanations, I'm going to attempt to supply one...
When Underscore's
bindAll
method is invoked with the single argument (the object), all function type properties of that object no longer reference the original function but instead another that fixes the context.Since one of the object's properties of type function is
constructor
, which is a reference to the Backbone constructor function (with the property__super__
), the property will be overwritten with a new function. This then means thatobject.constructor
will no longer have a property__super__
.To work around this issue I used the following function as an alternative to Underscore's
bindAll
:It's almost identical to Underscore's version but adds any properties of the original function to the new function through use of
_.extend()
.Why not simply use this to call the super:
(I am separating to several lines for clarification, you can do the call in one line)
Reference: GetPrototypeOf
I patched Backbone with getConstructor(), which returns the constructor and is immune to _.bindAll.
Usage:
Implementation: