0

一个小时以来,伙计们一直在尝试。但无法弄清楚为什么没有调用 aspect.after/before 。这是我的代码。

 require([ "dojo/aspect", "dojo/on", "dojo/dom", "dojo/domReady!" ], function(
    aspect, on, registry, dom) {

   var callback = function() {
    alert("called by click");
     };
   var callback2 = function() {
    alert("called by click 2");
         };
   var my = {
    clicking : on(dom.byId("alertButton"), "click", callback)
     };

    aspect.after(my, "clicking", callback2);

 });

提前致谢。

4

2 回答 2

0

如果你想基于一个动作来添加额外的功能,你可以试试这个:

<input type="button" id="alertButton" value="click me" />
<input type="text" id="textboxMessage" value="x" />
<script>
    dojo.require("dojo.aspect");
    dojo.require("dojo.on");
    dojo.require("dojo.dom");

    var handler = {};
    handler.alertClick = function(){ console.log('first callback'); };
    handler.msgBoxChange = function(){ console.log('a textbox value changed'); };
    handler.serverListChg = function(){ console.log('dropdown menu selected'); };
    handler.additional = function(){ console.log('Additional callback'); };

    dojo.aspect.after(handler, "alertClick", handler.additional, true);

    dojo.on(dojo.byId('alertButton'), "click", handler.alertClick);
    dojo.on(dojo.byId('textboxMessage'), "change", handler.msgBoxChange);
</script>

在示例中,操作handler.additional将附加在handler.alertClick

于 2013-09-04T09:29:24.563 回答
0

也许你混淆了用法。

aspect.after或者aspect.before,它是基于方法执行附加功能。不是动作。

例子:

aspect.after(my, "clicking", callback2);

任何调用my.clicking()它的地方都会运行callback2

用于在运行时执行附加功能on(dom.byId("alertButton"), "click", callback)

我的英文不好,简单解释一下:

function a(){
    alert();
}

my.clicking = function(){ a(); };
aspect.after(my, "clicking", callback2);

//equals to
my.clicking = function(){ a(); callback2(); };

//but not equals to
function a(){
    alert();
    callback2();
}
于 2013-09-04T08:33:18.420 回答