I’m getting the error:
Uncaught TypeError: Cannot read property 'constructor' of undefined
When declaring the following class:
class ViewHelpers extends Backbone.Events
I can use the same syntax to extend Backbone.Router, Views, Model etc. Here is the compiled javascript which I wrote in a quick log to make sure Backbone.Events was there
__t('views').ViewHelpers = (function(_super) {
#how i know it is definied here
console.log(_super.trigger)
__extends(ViewHelpers, _super);
function ViewHelpers() {
return ViewHelpers.__super__.constructor.apply(this, arguments);
}
return ViewHelpers;
})(Backbone.Events);
So the line causing the error is
ViewHelpers.__super__.constructor.apply(this, arguments);
What is different about __extends() method that it would work for Backbone.View and not Backbone.Events?
That’s because
Backbone.Eventsis not a “class”, so it cannot be extended, it’s a “module” that can be mixed-in into other objects (see docs here). In JavaScript terms that means that it’s not aFunction, that can be called as a constructor (i.e.new Backbone.Eventswill throw an error), it’s just a plain JS object whose properties (methods) can be assigned to other objects to make them event dispatchers.In CoffeeScript, you can mix-in the
Backbone.Eventsinto your objects when they are created:Or you can just extend the class’ prototype and avoid having those methods as (own) properties of all
ViewHelpersinstances:These two approaches should work and let you instantiate and use ViewHelpers as event dispatchers: