Dojo方面取消原始方法

cs7cruho  于 2022-12-16  发布在  Dojo
关注(0)|答案(1)|浏览(191)

我使用dojo aspect.before在调用我的原始方法之前执行一些操作。然而,如果aspect.before方法中不满足某些条件,我会尝试取消原始方法,但无法取消该事件。

require(["dojo/_base/declare",
"dojo/_base/lang",
"dojo/aspect",
"dojo/dom",
"dojo/on"], 
function(declare, lang, aspect, dom, on) {
  aspect.before(target,"onSave", function(event){
     var mycriteria  = //some logic to determine this value;
     if(mycriteria == null || mycriteria == undefined){ 
         //cancel the "onSave" method.
         // if cancelling this is not possible, can I call "onCancel" method here that'll cancel this 
         //event?
     }
  });
}
rpppsulh

rpppsulh1#

aspect.around就是你要找的,它允许你替换原来的方法,并按照你自己的方式应用它。

require(["dojo/_base/declare", "dojo/_base/lang", "dojo/aspect", "dojo/dom", "dojo/on"], function(declare, lang, aspect, dom, on) {
    aspect.around(target, "onSave", function(originalOnSave) {
        return function newOnSave() {//this function receives the parameters the onSave normally would. 
            var myCriteria = true;
            if (myCriteria) {
                //invoke original
                originalOnSave.apply(this, arguments);
            } else {//do nothing
            }
        }
    });
});

相关问题