Dijit自定义Tooltip无法正常工作

时间:2014-04-15 07:04:42

标签: javascript dojo tooltip

我正在努力将Web应用程序从Dojo 1.3.1迁移到Dojo 1.9.3。该应用程序有很多自定义小部件。其中一个是HelpTip,它从dijit/Tooltip扩展而来。

现在,当我点击显示帮助提示的按钮时,我在下面显示的第2行上得到一个例外,它显示TypeError: this._showTimer.remove is not a function

if(this._showTimer){
    this._showTimer.remove();
    delete this._showTimer;
}

我从dijit / Tooltip(Dojo 1.9.1)的open函数复制了上面的代码。但是dijit.Tooltip(Dojo 1.3.1)有所不同,如下所示:

if(this._showTimer){
    clearTimeout(this._showTimer);
    delete this._showTimer;
}

当我调试应用程序时,它会在_showTimer变量中显示一些编号值。所以,你可以看到在1.3.1版本的应用程序中,clearTimeout得到一个数字,它工作正常。但是在1.9.3版本中,它试图调用编号值的remove方法,而我真的不知道这是什么。

修改
这是我的HelpTip.js文件的代码

    define([
    "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;
                } else {
                    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);
                    return;
                }

                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 + "<br><br>";
                        dijit._masterTT.containerNode.appendChild(this.closeNode);
                    }

                } else {
                    if (dijit._masterTT.containerNode != null) {
                        dijit._masterTT.containerNode.innerHTML = this._noHelpAvailable + "<br><br>";
                        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;
});

由于

1 个答案:

答案 0 :(得分:1)

在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)并使用widget的buildin计时功能。