I am trying to write a jQuery widget by model here . Here is a screenshot of the widget:
(function ($) { $.widget("ui.notification", { _create: function () { if (!this.element.hasClass("ntfn")) { this.element.addClass("ntfn"); } this.elTitle = this.element.append("<div class='ntfn-title'>Notifications</div>"); this.elTitle.click(this._titleClick) }, _titleClick: function () { console.log(this); } }); })(jQuery);
The problem here is the "this" volume inside the _titleClick method, inside the method this points to the title element. But I need it to point to the widget element.
I think one way to do this would be to use a wrapper class like
var that = this; this.elTitle.click(function() { that._titleClick.apply(that, arguments); });
Is this the best way to solve this problem, or is there a common template for solving this problem?
javascript jquery jquery-ui
Arun P Johny
source share