从 jsp 调用的 dojo 确认对话框小部件出现问题

Issue with dojo confirm dialog widget called from jsp

我有以下小部件:

FruitWidget.html

<div data-dojo-type="dijit/ConfirmDialog"
 data-dojo-attach-point="fruitWarningDialog"
 data-dojo-props="title:'Timeout Warning'"
 data-dojo-attach-event="execute:printTimeout">
        Press ok to print something!
</div>

FruitWidget.js

define([
"dojo/_base/declare",
"dijit/ConfirmDialog",
"dijit/_WidgetBase",
"dijit/_TemplatedMixin",
"dijit/_WidgetsInTemplateMixin",
"dojo/text!./templates/FruitWidget.html"
], function(declare, ConfirmDialog, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, template) {

return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
    templateString: template,

    showWarning: function() {
        this.fruitWarningDialog.show();
    },

    checkForTimeout: function () {
        console.log('check for timeout called!');
        var self = this;
        var timeUntilWarning = 30 * 1000; // 30 seconds
        setTimeout(function() {
            self.showWarning();
        }, timeUntilWarning);
    },

    startMyFunction: function () {
        this.checkForTimeout();
    },

    printTimeout: function () {
        console.log('Timeout occurred!');
    }
 });
});

我正在尝试从我的 jsp 中调用此小部件,如下所示:

<script>
  require(["mywidgets/widget/FruitWidget"], function(FruitWidget) {
    var fruitWidget = new FruitWidget();
    fruitWidget.startMyFunction();
  });
</script>

但是当 30 秒超时到期并且 showWarning 被调用时,我收到错误消息:

There was an error on this page.

Error: Uncaught TypeError: Cannot read property 'show' of undefined
URL: mywidgets/widget/FruitWidget/FruitWidget.js

我是 Dojo 的新手,无法理解这个问题。为什么 fruitWarningDialog 在模板中提到为 data-dojo-attach-point 时未定义?

data-dojo-attach-point 指的是 DOM 节点,而不是小部件本身。您将小部件附加到此节点,通常在 postCreate() 中。在您的情况下,您可以直接调用 show() 函数:

showWarning: function() {
    this.show();
},