We want to monkey patch (wrap, really) core Backbone objects. Let's use Backbone.Router
for this example.
The original Backbone.Router is wrapped as shown in duck_punch.js.
- The constructor function (Backbone.Router) is replaced with ours (which runs the console profiler around the invocation of the original function). L4-9
- Since we've overwritten Backbone.Router, we restore its prototype to be the original's prototype. L12
- We also need to restore any 'static' methods that existed directly on the original. L15-18
This works great in JS (or even CS) when the app routers are created using Backbone.Router.extend
.
However, when using CS extends
(AppRouter extends Backbone.Router
), our wrapped constructor is never invoked. This is due to fact that CS generates:
function AppRouter(){
_ref = AppRouter.__super__.constructor.apply(this, arguments);
return _ref;
}
where __super__
is Backbone.Router.prototype
. Since it is invoking Backbone.Router.prototype.constructor
, and not Backbone.Router
, our patched implementation is never invoked! #sadface