var JavascriptHelper = Backbone.Model.extend("JavascriptHelper",
{}, // never initialized as an instance
{
myFn: function() {
$('.selector').live('click', function() {
this.anotherFn(); // FAIL!
});
},
anotherFn: function() {
alert('This is never called from myFn()');
}
}
);
The usual _.bindAll(this, ...) approach won’t work here because I am never initializing this model as an instance. Any ideas? Thanks.
You could do it by hand:
Or, if
anotherFndoesn’t care whatthisis when it is called (or if it wantsthisto be whatliveuses):As an aside,
livehas been deprecated in favor ofon. Also, if you’re not instantiating your JavascriptHelper, then why is it a Backbone.Model at all? Why not use a simple object literal:And what are you expecting this construct:
to leave you in
JavascriptHelper? Extending a string is strange but passing three arguments toBackbone.Model.extendis pointless, it only cares about two arguments. If you want static properties then you should be passing them as the second argument: