【发布时间】:2015-10-16 09:08:09
【问题描述】:
每当我初始化一个新的小部件时,我都需要将一个自定义小部件添加到特定的 div。
我相信最标准的方法是使用链接
(new MyFirstWidget()).placeAt('myDiv');
我对这种方法并不是很热衷,因为它在每次初始化时都需要placeAt() 的详细信息,而在我的情况下,我需要始终强制将该类型的小部件嵌套在特定的 div 中。
相反,我想在小部件类中添加此信息。
目前我正在使用以下代码,在postCreate() 中使用placeAt() 工作正常。
我想知道:
-
postCreate()是正确的地方吗?可以在更好的地方添加 在小部件生命周期内? - 我注意到在
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);
}
});
});
【问题讨论】:
标签: javascript dojo