简体   繁体   English

Dojo:在事件侦听器回调中维护对窗口小部件的引用

[英]Dojo: maintain reference to widget in event listener callback

I have a Dojo widget that looks as follows: 我有一个Dojo小部件,如下所示:

define([
    // stuff...
], function(declare, lang, win, on, query,
    _WidgetBase, _TemplatedMixin, _OnDijitClickMixin, template){

    return declare("Toolbar", [_WidgetBase, _TemplatedMixin, _OnDijitClickMixin], {
        templateString: template,
        map: null,

        constructor: function(options) {
            // this.map set here
            lang.mixin(this, options);

            if (this.map === null) {
                console.log("Error: no map!");
            }
        },

        postCreate: function() {
            on(this.domNode, ".toolButton:click", this.loadTool);
        },

        // in the following function, this refers to element where event 
        // originated here   
        loadTool: function(e) {
            var clickedTool = "tools/Tool" + this.id[this.id.length - 1];

            require([clickedTool], function(Tool) {
                // Fails: want this.map to refer to map defined by constructor above
                (new Tool({ map: this.map })).placeAt(win.body());
            });
        }
    });
});

At the moment, this Toolbar widget is just an ordered list of buttons, each having class toolButton . 目前,此Toolbar小部件只是按钮的有序列表,每个按钮都有一个类toolButton When a button is clicked, it will conditionally load the module (which returns another widget) associated to the clicked button. 单击按钮时,它将有条件地加载与单击的按钮关联的模块(返回另一个小部件)。 For simplicity, the tool modules are named tools/Tool<some_number> where <some_number> is obtained from the id on the clicked button. 为了简单起见,工具模块被命名为tools/Tool<some_number> ,其中<some_number>是从单击按钮上的id获得的。

I need to pass the map property of the Toolbar widget into the constructor of the Tool widget as seen on this line: 我需要将通过map中的属性Toolbar小工具进入的构造Tool如看到此行窗口小部件:

(new Tool({ map: this.map })).placeAt(win.body());

However, as noted above, within loadTool , this refers to the button that was clicked and not to the Toolbar widget. 但是,如上所述,在loadToolthis是指单击的按钮,而不是“ Toolbar小部件。 One possibility would be to called the event listener as 一种可能性是将事件监听器称为

on(this.domNode, ".toolButton:clicked", lang.hitch(this, this.loadTool);

and then obtain the id of the clicked button from the event object...but this seems hacky and like the incorrect way to handle it. 然后从事件对象中获取被单击按钮的id ...但这似乎很hacky,并且喜欢处理它的错误方法。

What is the "best practice" for doing this? 这样做的“最佳实践”是什么?

The best practice is to pass the context to the callback function, like your second approach. 最佳做法是像第二种方法一样,将上下文传递给回调函数。 This allows you to control how a function executes, particularly in asynchronous operations. 这使您可以控制函数的执行方式,尤其是在异步操作中。

From dojo documentation: 从dojo文档中:

on(something, "click", processEvent);

In asynchronous callbacks such as above, the context that the code is executing in has changed. 在上述的异步回调中,代码在其中执行的上下文已更改。 It will no longer refer to the object that originally provided it, but its context will now refer to the enclosing object, the callback. 它将不再引用最初提供它的对象,但是其上下文现在将引用封闭的对象,即回调。 To get around this, you can use hitch() to force the function to retain its original context. 要解决此问题,可以使用hitch()强制该函数保留其原始上下文。

The same code done properly will look like: 正确完成的相同代码如下所示:

on(something, "click", lang.hitch(this, processEvent));

http://dojotoolkit.org/reference-guide/1.10/dojo/_base/lang.html#dojo-base-lang-hitch http://dojotoolkit.org/reference-guide/1.10/dojo/_base/lang.html#dojo-base-lang-hitch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM