Issue with dojo confirm dialog widget called from

2019-08-04 11:21发布

问题:

I have the following widget:

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!');
    }
 });
});

I am trying to invoke this widget from inside my jsp as below:

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

But when 30 second timeout expires and showWarning gets called, I get error as:

There was an error on this page.

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

I am new to Dojo and not able to understand the issue. Why fruitWarningDialog is undefined when it is mentioned as data-dojo-attach-point in template ?

回答1:

The data-dojo-attach-point refers to a DOM node, not the widget itself. You attach a widget to this node, typically in postCreate(). In your case, you could call the show() function directly:

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