The simple introduction of the previous article Smartjs some common methods of API. This introduction to the foundation of the Promiseevent (this name has not been thought well, was prepared to use callbacks, but to avoid confusion with jquery, St's namespace can be directly linked to $)
Promiseevent
Event-and promise-based callback management, similar to the callbacks of jquery, but with the functions of result passing, priority, event parameters, promise control, etc.
Interface method
var events = st.promiseevent (mode); Events.add (name,function(E,arg,......) { },priority,eventmode) Event.fire (Arg,...);
Parameter description
Mode:once and callback two modes, (callback mode does not add event arguments)
E.stopprogation () block subsequent callbacks
Event.add (Name,fn,priority,eventmode) Add event callback, Name: Added event callback names; priority: Weight setting; EventMode: Event mode added; once;
Event.fire (arg,,,,) execution event callback
Event.firewith (Context,[args]) using context callbacks
Event.has (name) determines whether a callback is registered based on the callback name
Event.remove (name) Delete callback by name
Event.clear () Clears all callbacks
E Event Parameters
E.result result of previous callback
E.remove () Delete the current callback
E.promise () return contract
E.resolve () Settlement contract
E.reject () Refusal of contract
Using the sample
Normal way
varcalls =st.promiseevent (), result= []; //the test uses once mode to perform a destroyCalls.add (' Call1 ',function(E, text) {Result.push (text+ ' 1 '); },"Once") Calls.add (' Call2 ',function(E, text) {//effect with "once"E.remove () result.push (text+ ' 2 '); }) //execution, results [call1,call2]Calls.fire (' Call ');
Weight, default weight is 0, can be set by st.conf ({defpriority:100}), same weight first in first out
result = []; //WeightsCalls.add (' P ',function(E, text) {Result.push (' Def '); }) Calls.add (' P10 ',function(E, text) {Result.push (10); },10) Calls.add (' P50 ',function(E, text) {Result.push (50); },50) Calls.add (' Pdef ',function(E, text) {Result.push (' Def2 '); }) //execution, results [50,10,def,def2]Calls.fire ();
Stopprogation, stop subsequent callbacks
// stopprogation, stop subsequent callbacks Calls.add (' Stop ',function(e) { e.stoppropagation (); Result.push ("Stop"); }, // execution, results [50,stop] Calls.fire ();
The result is passed, Promiseevent returns the result of the return or resolve non-undefined results are recorded and passed down;
// result delivery mode Calls.add (' C1 ', function (E, text) { return text + "-C1" ' C2 ', function (E, text) { return E.result + "-c2" ' C3 ', function (E, text) { return E.result + "-C3" // Execute, result test-c1-c2-c3 calls.fire (' test ');
Prmose mode,
Can be used in conjunction with the promise of jquery
$.when (Calls.fire ()). Done (function (result) {
})
//Clear Callbackcalls.clear (); Result= []; //Promise ModeCalls.add ("C1",function(e) {setTimeout (function() {Result.push ("C1"); E.resolve (); }, 100); returne.promise (); }); Calls.add ("C2",function(e) {Result.push ("C2"); }); //execution, results [C1,C2]Calls.fire ();
Mode settings, once (execute and destroy) and callback (simple callback)
// callback mode & once mode var calls2 = St.promiseevent ("callback Once"); // callback does not have an E event argument, but has a callback management that is seen function (text) { return text + "-c1"; }) // executes the first time, results test-c1 Calls2.fire (' Test '); // executes the second time, because the once mode, the result undefined Calls2.fire (' Test ');
For more examples, refer to the test cases on Smartjs