Dojo aspect cancel original method

293 Views Asked by At

I am using dojo aspect.before to perform some actions prior to calling my original method. However, I am trying to cancel the original method if some criteria is not met within the aspect.before method, but not able to cancel that event.

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?
     }
  });
}
1

There are 1 best solutions below

0
On

aspect.around is what you're looking for. It allows you to substitute the original method and apply it on your own terms.

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