Javascript DOJO正在获取小部件的id

Javascript DOJO正在获取小部件的id,javascript,dojo,Javascript,Dojo,我使用dojo.parse从html初始化一些小部件 我需要获取被初始化的小部件的ID,并将其传递给另一个函数。 使用var test=\u WidgetBase.id我无法获取id 你知道怎么解决吗 define([ 'dojo/_base/declare', 'dijit/_WidgetBase', 'dijit/_TemplatedMixin', 'dijit/_AttachMixin', 'dojo/t

我使用
dojo.parse
从html初始化一些小部件

我需要获取被初始化的小部件的ID,并将其传递给另一个函数。 使用
var test=\u 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。运行postMixInProperties后,ID将在小部件生命周期中可用:

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中执行。

感谢您的回答,我现在正在使用类似的解决方案,并在PostMixinProperty之前使用序言。你能推荐序言方法吗?对不起,在生命周期中忘记了序言,它在生命周期中仍然没有记录。我将在中编辑它。但在这种情况下,它对您没有帮助,因为它在构造函数之前运行,ID将不可用。您可以尝试在序言中生成自己的ID,也许这个答案对您有用: