sammy
sammy copied to clipboard
Calling Helpers outside of the event context
Today I have been using helpers and plugin patterns. I really like the way they work and they are generally far easier to understand and use, for newbies, than the raw jQuery $.extend functionality. The bit I got stuck with is trying to use helpers within then() blocks where you are outside of the event context: what I wanted to do is add a trigger pointing at a helper within a then() block:
this.partial('some.template', { attributes: defaults }) .then(function() { $('#clear').click(this.someHelperMethod); });
So, what I am trying to do is bind a click event to a helper to make the code easier to manage. The code above will not work as 'this' is the wrong object, the following examples will work but exhibit various degrees of complexity:
var that = this; this.partial('some.template', { attributes: defaults }) .then(function() { $('#clear').click(that.someHelperMethod); });
this.partial('some.template', { attributes: defaults }) .then(function() { $('#clear').click(App.context_prototype.prototype.someHelperMethod); });
this.partial('some.template', { attributes: defaults }) .then(function() { $('#clear').click(this.event_context.someHelperMethod); });
The documentation is pretty clear on this topic, although it has taken me a long time to understand, but I can't help but feel that doing something a little different here would be very valuable.