2

我无法弄清楚如何将 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
} );

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

4

1 回答 1

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
    }
  });
});
于 2012-11-20T17:31:37.053 回答