javascript - How can I build jQuery like events? -


I would like to create a function that is called some event handles based on a result, such as the $ Ajax () For example, you can define this Ajax code:

  $ Ajax ({url: "http: //domainname.tld", type: 'GET'}) done (function (e) {// succed}). Unsuccessful (work (e) {// error});  

I would like to receive this

  .done (function (e) {// succed}  

blocks To work with my function, I do something like this:

  function SendRequest (arg1, arg2, onSuccess, onError) {if (true) {onSuccess (true);} Else {OnError (false);}}  

and call it like this

  SendRequest ("someArg1", "someArg2", function (ReturnValue) {Alert (returnValue);}, function (returnValue) {warning (return value);});  

and want to call it like this:

< Pre> SendRequest ("someArg1", "someArg2 ") .onSuccess (function (return value) {warning (return value);} .property (function (return value) {warning (return value);});

Thanks for pointing in the direction!

Thanks and to search for the right keyword and more Finally solve my problem!

After reading the great article, I came up with the following solution:

  function asyncMethod (arg1, arg2) {var deferred = $ ; // Do your work if (arg1 == arg2) {// true results represents, alternative deferred.resolve (true); } Else {// something went wrong, reject (wrong still results, this is also optional) deferred.reject (false); } Withdrawal of withdrawal.Promis (); }  

and calling it this way:

  $ When (asyncMethod (true, false) .Done (function (returnValue) {alert (arg1 + "==" + arg2);}) .file (function (return value) {warning (arg1 + "! =" + Arg2 );}););  

Comments