Note that there are some explanatory texts on larger screens.

plurals
  1. PORefactoring many jQuery Ajax calls - best practice?
    primarykey
    data
    text
    <p>I have a lot of JavaScript/ jQuery code blocks to handle asynchronous data processing in my page. Each code block has three functions (code is incomplete and for illustrative purpose only):</p> <ol> <li><p>encapsulates <code>$.ajax</code> call:</p> <pre><code>function doSomething(data){ // do some preprocessing $.ajax({}); // some JQuery Ajax operation that accepts data // do some postprocessing return false; }</code></pre></li> <li><p>handles the response:</p> <pre><code>function handleResponse(result){ // process the result return false; }</code></pre></li> <li><p>handles any error:</p> <pre><code>function handleError(XMLHttpRequest, textStatus, errorThrown){ // gracefully handle the Error and present relevant information to user return false; }</code></pre></li> </ol> <p>In a page that requires a lot of data processing I end up having a lot of these blocks which seems to be duplication, so I decided to do some refactoring. </p> <p>I figure there would be different ways to go about this. </p> <ol> <li>One could have one error handler that can be reused across Ajax calls (obvious).</li> <li>One could maybe reuse some response handlers, but this would be akward as responses are very different depending on call.</li> <li>Maybe create some kind of prototype object that provides base functionality and have a static method for error handling (can this be done in JavaScript?).</li> </ol> <p>I was just wondering if anyone has come across this and/or if there is a best practice solution for this? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload