我无法弄清楚如何将dojo / aspect与小部件一起使用。
请考虑以下事项:
require( [ 'dijit/form/Button' ],
function( Button)
{
var myButton = new Button({label: 'Click me!'});
} );
我如何连接到按钮的postCreate()或startup()方法以发现它何时被渲染?
当我可以向方法添加建议时似乎没有意义。请参阅以下评论:
require( [ 'dijit/form/Button', 'dojo/aspect' ],
function( Button, aspect )
{
// I cannot use aspect.after() here as I have no instance yet
var myButton = new Button({label: 'Click me!'});
// ...but I cannot do it here either as the lifecycle has already kicked off
} );
(按钮只是为了更容易解释问题。我的现实问题涉及包含其他小部件的小部件,因此我需要知道在执行操作之前整个批次的呈现时间。)
答案 0 :(得分:1)
通过以编程方式实例化窗口小部件,隐式调用窗口小部件的postCreate
方法。据我所知,连接到小部件生命周期中的postCreate
阶段并不容易(或者真的有理由)。
startup
,另一方面,您需要在以编程方式实例化窗口小部件时显式调用:
var myButton = new Button({label: 'Click me!'});
aspect.after(myButton, 'startup', function(){
console.log('startup called');
});
//place button in page (since startup implies the widget has been placed in the page
myButton.placeAt(someDomNode)
myButton.startup();
如果您想在窗口小部件的postCreate生命周期中工作,您可能希望将该窗口小部件子类化。这样做会是这样的:
//in a file like my/custom/widget.js
define(['dojo/_base/declare','dijit/form/Button'],function(declare,Button){
return declare('my.custom.widget',[Button],{
postCreate:function(){
//we still want to call the parent class's postCreate function
this.inherited(arguments);
//create some other widgets as well
}
});
});