我正在努力将 Web 应用程序从 Dojo 1.3.1 迁移到 Dojo 1.9.3。该应用程序有很多自定义小部件。其中之一是 HelpTip
từ dijit/Tooltip
扩展而来。
现在,当我单击显示帮助提示的按钮时,我在下面所示的第 2 行中遇到异常,它显示 TypeError: this._showTimer.remove is not a function
.
if(this._showTimer){
this._showTimer.remove();
delete this._showTimer;
}
我已从 mở
复制了上面的代码dijit/Tooltip 的功能 (Dojo 1.9.1)。但 dijit.Tooltip (Dojo 1.3.1) 中存在差异,如下所示:
if(this._showTimer){
clearTimeout(this._showTimer);
delete this._showTimer;
}
当我调试应用程序时,它向我显示 _showTimer
中的一些编号值多变的。因此,您可以看到在 1.3.1 版本的应用程序中,clearTimeout 获取了一个数字并且工作正常。但在1.9.3版本中,它试图调用remove
数值方法,我真的不知道这是做什么的。
biên tập:
đây là của tôiHelpTip.js
的代码文件
định nghĩa([
"dojo/_base/array",
"dojo/_base/declare",
"dojo/_base/lang",
"dojo/dom",
"dojo/has",
"dojo/topic",
"dijit/Tooltip",
"dojo/on"
], function (array, declare, lang, dom, has, topic, Tooltip, on) {
var HelpTip = declare("tt.widget.HelpTip",Tooltip,{
// summary
// Pops up a tooltip (a help message) when you hover over a node.
showDelay: 0,
_noHelpAvailable: "Unable to load help for this item. Please contact support.",
api: null,
connectHandlers: function(/*Array*/ ids) {
array.forEach(ids, function(id) {
var node = dom.byId(id);
if (node && node.tagName.toLowerCase() != "html" && node.tagName.toLowerCase() != "head" && node.tagName.toLowerCase() != "body") {
this.connectId.push(node);
// For A11y we may need to work with onFocus as well
this.connect(node, "onclick", "_onClick");
if(has("ie")){
// BiDi workaround
node.style.zoom = 1;
}
}
}, this);
},
disconnectHandlers: function(/*Array*/ ids) {
if (ids) {
for (var i = this._connects.length - 1; i >= 0; i--) {
if (this._connects[i] && this._connects[i][0] && this._connects[i][0][0]) {
if (array.indexOf(ids, this._connects[i][0][0].id) != -1) {
this._connects[i].remove();
}
}
}
}
},
_onClick: function(/*Event*/ e) {
e.preventDefault(); // don't navigate!
this._onHover(/*Event*/ e);
},
_onHover: function(/*Event*/ e){
if (e.target.id == null || e.target.id == "") {
this.label = this._noHelpAvailable;
} khác {
this.label = "Retreiving help for this item...";
this.aroundNode = e.target;
var ids = e.target.id.split("_");
this.api.helpGetText(lang.hitch(this, this.helpGetText), ids[0], ids[1], ids[2]);
}
if(!this._showTimer){
var target = e.target;
this._showTimer = setTimeout(lang.hitch(this, function(){this.open(target)}), this.showDelay);
}
},
_onUnHover: function(/*Event*/ e){
// keep a tooltip open if the associated element has focus
if(this._focus){ return; }
if(this._showTimer){
clearTimeout(this._showTimer);
delete this._showTimer;
}
this.closeNodeConnect.remove();
this.close();
},
helpGetText: function(/*String*/ helpText, /*Object*/ error) {
if (error) {
topic.publish("/errorHandling/trapError", error);
trở lại;
}
this.closeNode = document.createElement("div");
this.closeNode.className = "fakeLink";
this.closeNode.innerHTML = "Close";
this.closeNodeConnect = this.connect(this.closeNode, "click", "_onUnHover");
if (helpText != null && helpText != "") {
if (dijit._masterTT.containerNode != null) {
dijit._masterTT.containerNode.innerHTML = helpText + "
";
dijit._masterTT.containerNode.appendChild(this.closeNode);
}
} khác {
if (dijit._masterTT.containerNode != null) {
dijit._masterTT.containerNode.innerHTML = this._noHelpAvailable + "
";
dijit._masterTT.containerNode.appendChild(this.closeNode);
}
}
// Firefox bug. when innerHTML changes to be shorter than previous
// one, the node size will not be updated until it moves.
dijit._masterTT.domNode.style.top = (dijit._masterTT.domNode.offsetTop + 1) + "px";
// position the element and change CSS according to position
var align = dijit._masterTT.isLeftToRight() ? {'BR': 'BL', 'BL': 'BR'} : {'BL': 'BR', 'BR': 'BL'};
var pos = dijit.placeOnScreenAroundElement(dijit._masterTT.domNode, this.aroundNode, align);
this.aroundNode = null;
dijit._masterTT.domNode.className="dijitTooltip dijitTooltip" + (pos.corner=='BL' ? "Right" : "Left");//FIXME: might overwrite class
}
}
);
return HelpTip;
});
Cảm ơn
在 Dojo 1.9 中,this._showTimer 是一个函数,但您的代码将其替换为 setTimeout 句柄。 if应该返回的函数定义在_WidgetBase(this.defer)中。在Tooltip的源代码中可以看到这样的代码:
_onHover: function(/*DomNode*/ target){
// summary:
// Despite the name of this method, it actually handles both hover and focus
// events on the target node, setting a timer to show the tooltip.
// tags:
// private
if(!this._showTimer){
this._showTimer = this.defer(function(){ this.open(target); }, this.showDelay);
}
},
这意味着 this._showTimer 连接到 _widgetBase 中定义的函数。该函数是 setTimeout 的某种包装函数,以防止小部件已被销毁时发生事件。我建议使用 this.inherited(arguments) 并使用小部件的内置计时函数。
Tôi là một lập trình viên xuất sắc, rất giỏi!