dojo:aspect.after不起作用

时间:2013-09-04 07:39:07

标签: dojo

一个小时以来,伙计们一直在努力。但无法弄清楚为什么没有调用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);

 });

提前致谢。

2 个答案:

答案 0 :(得分:0)

也许你对这种用法感到困惑。

aspect.afteraspect.before,它根据方法执行附加功能。不是行动。

示例:

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

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

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

NOT

我的英语不好,解释简单:

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

答案 1 :(得分: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

之后追加