DOJO 获取小部件的 ID

DOJO getting id of a widget

本文关键字:ID 小部 获取 DOJO      更新时间:2023-09-26

我正在使用dojo.parse来初始化HTML中的一些小部件。

我需要获取被初始化并传递给另一个函数的小部件的 ID。使用var test = _WidgetBase.id;我无法获得 ID。

知道如何解决吗?

   define([
        'dojo/_base/declare',
        'dijit/_WidgetBase',
        'dijit/_TemplatedMixin',
        'dijit/_AttachMixin',
        'dojo/text!./templates/Button.html',
        'dojo/dom-style',
        'dojo/_base/fx',
        'dojo/_base/lang',
        'dojo/on',
        'dojo/mouse',
        'require',
        'ntv/WebpartInitializer',
    ], function (declare, _WidgetBase, _TemplatedMixin, _AttachMixin, template, domStyle, fx, lang, on, mouse, require, WebpartInitializer) {
        // automatically generated properties from state
        var test = _WidgetBase.id; // PROBLEM HERE
        var webpartInitializer = new WebpartInitializer(test);
        var autoProperties = webpartInitializer.getProperties(),
        // custom properties
        customs = {
            templateString: template,
            ntvType: 'Button',
            baseClass: "button",
            postCreate: function () {
                var domNode = this.domNode;
            },
            _setTitleAttr: function () {
                console.log('++++++ test');
            }
        };
        // create new class mixin
        return declare([_WidgetBase, _TemplatedMixin, _AttachMixin], lang.mixin(customs, autoProperties));
    });

由于您的小部件尚未初始化,因此尚未分配 ID。该 ID 将在运行 postMixInProperties 后的小部件生命周期中可用:

require([
    'dojo/_base/declare',
    'dijit/_WidgetBase',
], function (declare, _WidgetBase) {
    declare("CustomWidget", [
        _WidgetBase
    ], {
        preamble: function () {
            console.log('preamble: ' + this.id);
        },
        constructor: function () {
            console.log('constructor: ' + this.id);
        },
        postMixInProperties: function () {
            console.log('postMixInProperties: ' + this.id);
        },
        buildRendering: function () {
            console.log('buildRendering: ' + this.id);
        },
        postCreate: function () {
            console.log('postCreate: ' + this.id);
        },
        startup: function () {
            console.log('startup: ' + this.id);
        }
    });
    var customWidget = new CustomWidget();
    customWidget.startup();
});

给出以下结果:

preamble:
constructor:
postMixInProperties: 
buildRendering: CustomWidget_0
postCreate: CustomWidget_0
startup: CustomWidget_0

因此,如果您需要使用该ID执行某些操作,则必须在buildRendering,postCreate或startup中执行此操作。