Javascript 如何将dojo/aspect连接到小部件?

Javascript 如何将dojo/aspect连接到小部件?,javascript,dojo,Javascript,Dojo,我很难弄清楚如何将dojo/aspect与小部件一起使用 考虑以下几点: require( [ 'dijit/form/Button' ], function( Button) { var myButton = new Button({label: 'Click me!'}); } ); 如何连接到按钮的postreate()或startup()方法以发现它何时被呈现 似乎没有必要在方法中添加建议。见以下评论: require( [ 'dijit/form/Button', 'dojo

我很难弄清楚如何将dojo/aspect与小部件一起使用

考虑以下几点:

require( [ 'dijit/form/Button' ],
function( Button)
{
    var myButton = new Button({label: 'Click me!'});
} );
如何连接到按钮的postreate()或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
} );

(该按钮只是为了更容易解释问题。我的实际问题涉及到包含其他小部件的小部件,因此我需要知道在执行操作之前整个小部件的渲染时间)。

通过编程方式实例化小部件,小部件的
postCreate
方法被隐式调用。据我所知,连接到小部件生命周期中的
post-create
阶段并不容易(或者说是一个很好的理由)

启动
,另一方面,当以编程方式实例化小部件时,需要显式调用:

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();
如果您想在小部件的后期创建生命周期中进行工作,您可能需要对该小部件进行子类化。这样做看起来像这样:

//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
    }
  });
});

我想要连接到postCreate阶段的原因是小部件的postCreate方法以编程方式在那里实例化它自己的子小部件。我需要在执行操作之前完全呈现这些子窗口小部件。无论如何,谢谢你的回答,但我想我必须创建一个变通方法。我编辑了我的答案,以找到一种可能的方法来做你想做的事情。还有其他方法可以完成同样的事情,但我认为建议的方法是dojo最常用的方法。@voidstate您在
create
postmixinproperty
buildRendering
中执行过异步功夫吗?因为否则代码是同步的,小部件应该在实例化后立即完全创建(在内存中,不一定呈现)。在postCreate方法中,我执行两级require()调用,每个调用嵌套在一个循环中,最后以编程方式将小部件添加到ContainerCode。然后我需要展示整件事。我通过使用dojo.Deferred.then()进行显示解决了这个问题,在加载最后一个require()时解决了这个问题。