Saturday 15 May 2010

javascript - Recommended location of business logic after an $.ajax call -


First of all, I kept all my magic as a call from the success function but I After realizing the call, did (...) add (and I believe there is also an ready ).

Could Business be Magic?

  $ Ajax ({url: "http: // ...", data type: 'jsonp', success: work () {doMagic (1);}, ready: function () {doMagic (2);}, .. .}) Done (function () {doMagic (3);});    

I do not know what is the "correct way", but most of what I have seen It is advisable to use .done (...) because it is clean and easy to read because the reason for the actual code is between the block of code success takes the actual AJAX call to the function.

In other words, it is easy to read:

  $ Ajax ({url: "http: // ...", data type: 'jsonp', ...}) done (function (data) {// some very long function here}}  < / Pre> 

To read it:

  $. Ajax ({url: "http: // ...", Data type: 'jsonp', success: function (data) ) {// here is a very long function}, ...}); It is said that if the function actually being said is really something important (i.e. just by dumping the result in some HTML element), I use a named function and its number From here, and in that case I think it will be easy to read:  
  function onsutus (data) {// some very long work here} $ .ajax ( {Url: "http: // ...", Datatype: 'JasonP', success: onsut, ...});   

To read it: < Pre> $. Ajax ({url: "http: // ...", data type: 'jsonp', ..}} done (onsut);

(In fact, it would be better for the names of your business logic (and all ) names like Business.Model.OnSuccess , but Anne left him because it is not really part of the North)

No comments:

Post a Comment