JS in a lot of nesting functions, very cow fork, then why to flatten?
Understand (oneself and others)
Easy to modify (oneself and others)
Normal Ajax invocation notation (based on jquery)
$.post (' url ', Jsonobj,
function (data)
{
if (data)
{
var tips = $.ligerdialog.tip ({title: ' Tip '), Content: ' Operation successful! '});
settimeout (function () {tips.close ();}, \ n);
}
else
{
var tips = $.ligerdialog.tip ({title: ' Tip ', Content: ' Operation failed! '});
settimeout (function () {tips.close ();},%);
}
}
);
What are the disadvantages?
After the function is nested, it is difficult to understand
function nesting, a line of function calls written a lot of lines, it is easy because of commas, parentheses and so on caused by syntax errors
jquery and Liger in the application code strong coupling, if you want to replace the UI framework, need to do a carpet search ...
Add delay attribute-Deferred
var Ajaxhandler = $.post (' url ', params);
Ajaxhandler.done (checkserverresponse);
var checkserverresponse=function
{
if (result)
{
var tips = $.ligerdialog.tip ({title: ' Tip '), Content:
' Operation successful! '});
settimeout (function () {tips.close ();}, \ n);
}
else
{
var tips = $.ligerdialog.tip ({title: ' Tip ', Content:
' Operation failed! '});
settimeout (function () {tips.close ();},%);
}
}
Explanation:
The JS after the change is not very different from the previous: If JS uses oo way to write, and then look at this code is very clear (you can simply interpret Checkserverresponse as a private class method, it will be easy to understand the writing intent)
What if there are multiple AJAX requests? Can be written as $.when ($.post (' Url1 '), $.post (' url2 '). Done (this.checkserverresponse); similar to the meaning of merging, and The parameter of the Checkserverresponse function is 2, corresponding to the result of the 2 AJAX requests respectively
What if there are multiple follow-up requests? Can be written as $.when ($.post (URL)). Then (Handler1). Then (Handler2). Done (Successhandler). Always (Alwayshandler). Fail ( Failhandler);