我正在为一个项目开发一个键盘小部件,我正在扩展$.ui.mouse
。我需要点击行为(不是由_mouseCapture触发)。我很难把this.options
放回里面。请看下面的代码块:
$.widget("ui.keyboard", $.ui.mouse, {
widgetEventPrefix: "keyboard",
options: {
[...]
},
[...],
_create : function() {
this.element.bind('click.'+this.widgetName, this._mouseClick);
},
_mouseClick: function(e) {
// how do i get this.options here
}
});
这样做最好吗:
$.widget("ui.keyboard", $.ui.mouse, {
widgetEventPrefix: "keyboard",
options: {
[...]
},
[...],
_create : function() {
var self = this;
this.element.bind('click.'+this.widgetName, function(e) { self._mouseClick(e, self); });
},
_mouseClick: function(e, self) {
// how do i get this.options here -> self.options
}
});
或者这样:
$.widget("ui.keyboard", $.ui.mouse, {
widgetEventPrefix: "keyboard",
options: {
[...]
},
[...],
_create : function() {
this.element.bind('click.'+this.widgetName, this._mouseClick);
},
_mouseClick: function(e) {
var self = $.data(this, "keyboard");
// how do i get this.options here -> self.options
}
});
当我尝试手动触发最后一个时,我遇到了问题,除非我点击this.element
(这是非常符合逻辑的),否则$.data是不会打开的。
还有别的办法吗?哪个是最好的选择?
谢谢,
发布于 2011-02-14 04:01:08
您可以调用一个返回对象的匿名函数,如下所示:
$.widget("ui.keyboard", $.ui.mouse, function(){
var self;
return {
widgetEventPrefix: "keyboard",
options: {
[...]
},
_create : function() {
self = this;
this.element.bind('click.'+this.widgetName, function(e) { self._mouseClick(e, self); });
},
_mouseClick: function(e, self) {
// do something with self.options
}
}
}());
这样,您就可以从_mouseClick-
https://stackoverflow.com/questions/4986434
复制相似问题