Since I can't put markdown here in the description... read the problem.md
# class Workspace extends Backbone.Router
Workspace = Backbone.Router.extend
routes:
'*filter': 'setFilter'
setFilter: (param) ->
app.TodoFilter = param || ''
app.todos.trigger('filter')
app.TodoRouter = new Workspace()
class Workspace extends Backbone.Router
# Workspace = Backbone.Router.extend
routes:
'*filter': 'setFilter'
setFilter: (param) ->
app.TodoFilter = param || ''
app.todos.trigger('filter')
app.TodoRouter = new Workspace()
# this flavor of the Router is NOT profiled.
# The CoffeeScript __extends implementation creates a new object as the
# Router instance and then replaces the constructor function with one of its own.
# The new constructor function invokes the Backbone.Router.prototype's constructor function.
# `_ref = Workspace.__super__.constructor.apply(this, arguments);`
# This means our replacement Backbone.Router constructor function (with the profiling bookends) is never invoked.
// given Backbone has loaded, let's profile all the views
(function(yeOldeBackboneRouter){
window.Backbone.Router = function(){
console.profile();
var instance = yeOldeBackboneRouter.apply(this, arguments);
console.profileEnd();
return instance;
};
// restore prototype
window.Backbone.Router.prototype = yeOldeBackboneRouter.prototype;
// restore own methods
var methods = Object.keys(yeOldeBackboneRouter);
for ( m in methods ) {
window.Backbone.Router[methods[m]] = yeOldeBackboneRouter[methods[m]];
}
})(Backbone.Router);
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.
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