在特定节点附加 Widget

Attaching a Widget at a specific node

我需要在每次初始化新小部件时将自定义小部件添加到特定 div。

我相信最标准的方法是使用一些东西 link

 (new MyFirstWidget()).placeAt('myDiv');

我不太喜欢这种方法,因为它在每次初始化时都需要 placeAt() 的详细信息,在我的情况下,我需要强制将该类型的小部件嵌套在特定的 div all时间。

相反,我想在小部件 class 中添加此信息。

目前我正在使用以下代码,在 postCreate() 中使用 placeAt() 工作正常。

我想知道:

  1. postCreate() 是正确的地方吗?可以在更好的地方补充 在小部件生命周期中?
  2. 我注意到在 postCreate() 中调用 this.placeAt() 之前,小部件被标记为 rendered,而实际上 没有被渲染,因为还没有被添加到 DOM 中......为什么 是吗?

  define([
        'dojo/_base/declare',
        'dojo/dom-construct',
        'dijit/_WidgetBase',
        'dijit/_TemplatedMixin',
        'dojo/text!./templates/PanelBasic.html'
    ], function (
        declare,
        domConstruct,
        _WidgetBase,
        _TemplatedMixin,
        template
        ) {
        'use strict';
        var attachTo = 'myPanels';
        return declare([_WidgetBase, _TemplatedMixin], {
            templateString: template,
            ntvType: 'Panel',
            constructor: function () {
            },
            postCreate: function () {
                this.inherited(arguments);
                this.placeAt(attachTo);
            }
        });
    });

postCreate 似乎是生命周期中的合适点。不过,您可能不想忘记先在其中调用 this.inherited(arguments)

_rendered 标志是 dijit/_TemplatedMixin 的内部标志,在 buildRendering 运行后设置为真,它负责从小部件的模板创建 DOM 元素。从这个意义上说,它已经 "rendered"(从模板字符串到 DOM),尽管它还没有进入文档流。