DOJO得到了一个小部件的id

时间:2014-12-15 10:16:25

标签: javascript dojo

我正在使用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));
    });

1 个答案:

答案 0 :(得分:2)

由于您的窗口小部件尚未初始化,因此尚未分配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中执行此操作。