Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have been adding callbacks to my functions like this...</p> <pre><code>function doThis( cb ) { // do stuff here if ( typeof cb === 'function' ) cb(); } function thenThis() { // callback functionality } doThis( thenThis ); </code></pre> <p>EDIT:</p> <p>Sorry, I really should have read through what you needed to do a bit more...</p> <p>When is a great option for handing pure ajax calls, but, if extra functionality needs to happen in the callbacks, i have found a different patter that helps me organize my code...</p> <pre><code>// Log_in_user // if response received and call was successful // get_user's_data // if response received and call was successful // alert user var ajax = { logIn : function( cb ) { $.ajax({ // params }).success( parseCB ).error( this.errorCB ); function parseCB( doc ) { // logic if ( typeof cb === 'function' ) cb(); } }, userData : function( cb ) { $.ajax({ // params }).success( parseCB ).error( this.errorCB ); function parseCB( doc ) { // logic if ( typeof cb === 'function' ) cb(); } }, errorCB : function(){ // logic } }, my = { init : function() { ajax.logIn( this.done.logIn ); if ( typeof cb === 'function' ) cb(); }, done : { logIn : function() { ajax.userData( this.done.userData ); }, userData : function() { // Do stuff with user data, then run my.done.page (the final callback) alert( 'user data recieved' ); }, page : function() { // set up clicks, or whatever else you need alert( 'everything is done' ); } } }; $( function() { my.init( my.done.page ); }); </code></pre> <p>This may seem like a bit much, but my thoughts with this pattern is to keep all my callback and actual function calls within the document ready, and within the my variable. jQuery is my framework of choice, so i put it out there as i would...i'm open to thought here, thanks.</p>
 

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