Trigger
With the trigger function on the target object, the method of the target object will have the method injection function, the injected way has before (pre-execution), after (post-execution) and round (surround), there is also a exception mode, because the exception processing module has not been completed, temporarily empty first.
Interface method
St.attachtrigger (target, mode, fninterface); //post-injection methodTarget.on (name, Trname, FN, priority, EventMode)//Pre-placement injection methodTarget.onbefore (name, Trname, FN, priority, EventMode)//Method Wrappingtarget.onround (name, Trname, FN)//Delete Injection MethodTarget.off (name, trname)//Remove the predecessor injection methodTarget.offbefore (name, trname)//Replace the original method of the target objectTarget.extend (prop)
Parameter explanation
Target: Destination object;
Mode: Callback mode, mode setting with promisevent
IFace: Custom interface method; After using the Attachtrigger method, some control methods are attached to target, in order to avoid duplicate names and control external methods, use IFace to customize
After using Attachtrigger, the method of injecting control on target is appended, on,onbefore and so on
Name: The method name of the target to be injected
Trname: Add trigger Callback Name
fn: Injection method, method parameter with prmiseevent callback parameter
Priority: Weight setting, with prmiseevent;
EventMode: Join the event pattern, with Prmiseevent
It is important to note that the on and Onbefore injection method interfaces are FN (E,arg,...)
E with the promiseevent parameter set, the only difference is that there are two newly added controls:
E.preventdefault blocks the default method, which is the original method of target; But does not affect the callback event
E.stop () stops the execution of the method, either the original method or the callback;
The Onround is FN (Originalfn,arg,..), and the surround and front-to-back does not conflict
ORIGINALFN: A method to surround;
Using the sample
Front and rear
varresult = []; varobj ={test:function(name) {Result.push (name); } }; //Join TriggerSt.attachtrigger (obj); //Adding a pre-built methodObj.onbefore ("Test", "Addbefore",function(d, name) {Result.push (' before-' +name)}); //adding a post-placement methodObj.on ("Test", "Addafter",function(d, name) {Result.push (' after-' +name)}); //execution, result "Before-bind,bind,after-bind"Obj.test (' bind ');
Custom Interface Iface
var obj1 = St.attachtrigger ({ function(name) { Result.push (name); } } , { // Mask Post methodon null ,// Set the predecessor method Onbefore to bind onbefore: "Bind" }) Obj1.bind (function(d, Name) { Result.push (' before-' + name); });
Method Wrapping
var obj3 = St.attachtrigger ({ function(name) { Result.push (name); } }); obj3.onround (function(FN, name) { Result.push (' before '); FN (name); Result.push (' after '); }); Obj3.test (' round '); Expect (Result.join (', ')). ToBe ("Before,round,after");
Promise Control
Both the pre-and post-promiseevent are controlled by the event parameters of the promise, but in the original method, in order to avoid the confusion caused by the intervention of the original method, the event parameters are not added, but are controlled by using jquery's deffered.
varobj =St.attachtrigger ({test:function(name) {//deferred using jquery in the original method varD = $. Deferred (); SetTimeout (function() {Result.push (name); D.resolve (); }, 100); returnd.promise (); } }); Obj.onbefore (' Test ', ' Testbefore ',function(d, name) {SetTimeout (function() {Result.push (name+ '-before '); D.resolve (); }, 100); returnd.promise (); }) Obj.on (' Test ', ' Testafter ',function(d, name) {SetTimeout (function() {Result.push (name+ '-after '); D.resolve (); }, 100); returnd.promise (); }) $.when (Obj.test ("Call"). Done (function() {Expect (Result.join (', '). ToBe (' Call-before,call,call-after ').); })
Result delivery
varobj =St.attachtrigger ({//Promise Parameter PassingTestfunction(name) {varD = $. Deferred (); SetTimeout (function() {d.resolve (name+ '-base '); }, 100); returnd.promise (); }, //return result PassTestreturn:function(name) {returnName + "-base"}}); Obj.on (' Test ', ' Testafter ',function(d, name) {SetTimeout (function() {d.resolve (D.result+ '-after '); }, 100); returnd.promise ();}) Obj.on (' Testreturn ', ' testafter ',function(d, name) {SetTimeout (function() {d.resolve (D.result+ '-after '); }, 100); returnd.promise ();}) $.when (Obj.test ("Call"). Done (function(data) {expect (data). ToBe (' Call-base-after ');}); $.when (Obj.testreturn (' Call2 '). Done (function(data) {expect (data). ToBe (' Call2-base-after ');});
Stop control
varobj =St.attachtrigger ({test:function(name) {Result.push (name); }}); Obj.onbefore (' Test ', ' Testbefore ',function(d, name) {//Stop subsequent executionsD.stop (); Result.push (Name+ '-before1 ');}) Obj.onbefore (' Test ', ' Testafter ',function(d, name) {Result.push (name+ '-before2 ');}) Obj.on (' Test ', ' TestBefore2 ',function(d, name) {Result.push (name+ '-after ');}) Obj.test (' Call '); expect (Result.join (', ')). ToBe (' Call-before1 ');
Stoppropagation and Preventdefault Control
varobj =St.attachtrigger ({test:function(name) {Result.push (name); } }); Obj.onbefore (' Test ', ' Testbefore ',function(d, name) {Result.push (name+ '-before1 '); //Stoppropagation block before callback, Preventdefault block default methodd.stoppropagation (). Preventdefault (); }) Obj.onbefore (' Test ', ' Testafter ',function(d, name) {Result.push (name+ '-before2 '); }) Obj.on (' Test ', ' TestBefore2 ',function(d, name) {Result.push (name+ '-after '); }) Obj.test (' Call '); //final output of the first before back and reset callbackExpect (Result.join (', ')). ToBe (' Call-before1,call-after ');
For more examples, refer to the test cases on Smartjs