Sto provando a scrivere un widget jQuery seguendo il modello dato here. Ecco un'istantanea del widget:Come gestire gli eventi nei widget dell'interfaccia utente jQuery
(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);
Qui il problema è con lo scopo di "questa" all'interno del metodo _titleClick
, all'interno del metodo questa punta all'elemento title
. Ma ho bisogno che indichi l'elemento widget
.
Penso che un modo di farlo sarà quello di utilizzare una classe wrapper come
var that = this;
this.elTitle.click(function() {
that._titleClick.apply(that, arguments);
});
E 'questo il modo migliore per risolvere questo problema o c'è qualche schema generale per risolvere questo problema?
Grazie. Questo e 'esattamente quello che stavo cercando. – jwanga