0

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?
     }
  });
}
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
Le_Master
  • 147
  • 1
  • 2
  • 20
  • Regarding the former `AOP` tag, wrapping and reassigning already declared functionality (be it functions or methods) misses any aspect of _AOP_. Any language which wants to qualify for the latter has to provide abstraction levels for at least `Joinpoint`, `Advice` and `Aspect`. The use case described by the OP should be referred to as method modification, and JavaScript of cause is well suited for this scenario and could easily provide a complete `target`/`context` aware toolset of method modifiers like `around`, `before`, `after`, `afterThrowing` and `afterFinally` via `Function.prototype`. – Peter Seliger Sep 14 '22 at 14:25

1 Answers1

1

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
            }
        }
    });
});
Ivo Jonker
  • 306
  • 3
  • 9