1/* 2========= 3joms.ajax 4========= 5 6joms.ajax = jax + jQuery.ajax(). 7Jax is like the protocol. jQuery.ajax() is like the transport. 8 9Process flow: 10Jax wraps data. 11 | 12jQuery.ajax() sends the data to server. 13 | 14Server returns jax responses. 15 | 16jQuery.ajax() delegate jax responses. 17 | 18jax.processResponse() executes jax response. 19 | 20jQuery.ajax() extract callback data 21embedded within jax responses. 22 | 23jQuery.ajax() pass the callback data to 24error, success & complete handler. 25 26This is done without modifying neither jax or jQuery.ajax() library. 27So existing jax.call() can execute side-by-side without problems. 28 29============================ 30How to use joms.ajax.call()? 31============================ 32 33joms.ajax.call(sFunc, sArgs, callback) 34joms.ajax.call('controller,function', [arg1, arg2], function(data) { 35 // What to do when the ajax call is successful 36}); 37 38joms.ajax.call(sFunc, sArgs, settings) 39joms.ajax.call('controller,function', [arg1, arg2] { 40 success: function(data) { 41 // What to do when the ajax call is successful 42 }, 43 error: function(data) { 44 // What to do when the ajax call is not successful 45 // e.g. timeout, 404, 500, etc. 46 }, 47 48 // For more settings to fiddle with, look up jQuery.ajax() docs. 49}); 50 51 52=========================================== 53Advantage of joms.ajax() with example usage 54=========================================== 55 561. Ability to use PHP associative array as JSON as callback data, e.g. 57 58 // ---- PHP side --- // 59 $callbackData['url'] = 'http://www.jomsocial.com' 60 $callbackData['caption'] = 'JomSocial' 61 62 $objResponse->addScriptCall('__callback', $callbackData); 63 64 // ---- JS side --- // 65 joms.ajax.call('controller,function', [arg1, arg2], function(data) 66 { 67 console.log(data.url); // output http://www.jomsocial.com 68 console.log(data.caption); // output JomSocial 69 }); 70 712. Ability to handle server timeouts or other errors, e.g. 72 73 joms.ajax.call('apps,ajaxSetOrder', [appOrder], { 74 success: function(data) { 75 // Remove saving indicator 76 jQuery('#app-position').removeClass('onSave'); 77 }, 78 error: function(data) { 79 // Revert app ordering 80 jQuery('#app-position').sortable('cancel'); 81 } 82 }); 83 843. Continue writing relevant codes within the same function in JS, 85 instead of writing them as addScriptCall() in PHP, while 86 preserving the function/variable scope those relevant codes 87 could access and reuse, e.g. 88 89 function removePhoto() 90 { 91 var photo = jQuery('#photo-63'); 92 var photoId = photo.attr('id').split('-')[1]; 93 94 joms.ajax.call('photos,ajaxRemovePhoto', [photoId], function(data) 95 { 96 // Now I can use back the 'photo' variable 97 // $objResponse->addScriptCall('jQuery("#photo-' + $photoId + ')').remove'); 98 99 jQuery(photo).remove(); 100 }); 101 } 102*/ 103 104joms.extend({ 105 ajax: { 106 execute: function(o) 107 { 108 // Rearrange jQuery.ajax callback order. 109 // jQuery.ajax : beforeSend -> dataFilter -> success/error -> complete 110 // joms.ajax : beforeSend -> beforeDataFilter -> dataFilter -> afterDataFilter -> fail/success/error -> complete 111 // 112 // joms.ajax callback handlers 113 // - beforeSend: function(xhr) 114 // - fail : function(xhr, status, e) 115 // - beforeDataFilter: function(jaxData) 116 // - afterDataFilter : function(jomsData) 117 // - success : function(arg1, arg2...) // from $objResponse->addScriptCall('__callback', $arg1, $arg2); 118 // - error : function(arg1, arg2...) // from $objResponse->addScriptCall('__throwError', $arg1, $arg2); 119 // - complete : function(xhr, status) 120 var settings = {}; 121 joms.jQuery.extend(settings, o); 122 123 settings.error = function() 124 { 125 if (o.fail) o.fail.apply(this, arguments); 126 } 127 128 settings.dataFilter = function(jaxData, type) 129 { 130 if (o.beforeDataFilter) o.beforeDataFilter(jaxData); 131 132 // Delegate jax commands to jax.processReponse() 133 jax.processResponse(jaxData); 134 135 // Extract callback data embedded inside jaxData 136 var jomsData = { 137 success: joms.ajax.extractJomsData(jaxData, '__callback'), 138 error : joms.ajax.extractJomsData(jaxData, '__throwError') 139 } 140 141 if (o.afterDataFilter) o.afterDataFilter(jomsData); 142 143 return jomsData; 144 } 145 146 settings.success = function(jomsData, status) 147 { 148 if (jomsData.error.length>0 && o.error) 149 { 150 joms.jQuery.each(jomsData.error, function(i, args) 151 { 152 o.error.apply(this, args); 153 });
154 return; 155 } 156 157 if (jomsData.success.length>0 && o.success) 158 { 159 joms.jQuery.each(jomsData.success, function(i, args) 160 { 161 o.success.apply(this, args); 162 }); 163 } 164 } 165 166 // Create a dummy __callback function so we don't have 167 // waste computing cycles trying to filter them out in 168 // filterJaxResponse(); 169 window.__callback = window.__throwError = function() {}; 170 171 // Use jQuery.ajax() (replacing the not-so-fun jax.submitTask()) 172 joms.jQuery.ajax(settings); 173 }, 174 175 /* 176 * joms.ajax.call() 177 * 178 * @param sFunc {String} The server function to call in the format of "controller,function" 179 * @param sArgs {Array} The arguments to pass to the server function 180 * @param o {Function/Object} If passed as a function, it is treated as callback function 181 * that gets executed by 'success' event. 182 * If passed as an object, it is treated as ajax settings. 183 */ 184 call: function(sFunc, sArgs, o) 185 { 186 var settings = { 187 url: jax_live_site, 188 type: 'POST', 189 data: { 190 option : 'community', 191 task : 'azrul_ajax', 192 no_html: 1, 193 func : sFunc 194 }, 195 dataType: 'text' 196 }; 197 198 // To fix CSRF issues. 199 settings.data[ jax_token_var ] = 1; 200 201 // Build Jax-style arguments for the url param 202 joms.jQuery.extend(settings.data, this.buildSArgs(sArgs)); 203 204 // If o is callback function, wrap it back as an ajax settings object. 205 if (typeof o=='function') 206 o = {success: o}; 207 208 // Override default settings with provided settings 209 joms.jQuery.extend(settings, o); 210 211 this.execute(settings); 212 }, 213 214 /* 215 * joms.ajax.buildSArgs() 216 * [Internal function] 217 * 218 * Modified from jax.call(). 219 * Instead of building arguments into a string, 220 * this function builds arguments into a key/value object, 221 * and jQuery.ajax() function will serialize them. 222 * 223 * @param sArgs {array} An array of arguments to pass into the server function. 224 */ 225 buildSArgs: function(sArgs) 226 { 227 var data = {}; 228 229 joms.jQuery(sArgs).each(function(i, a) 230 { 231 if (typeof a=='string') 232 a.replace(/"/g, '"'); 233 234 if (!jax.isArray(a)) 235 a = new Array('_d_', encodeURIComponent(a)); 236 237 data['arg'+i] = jax.stringify(a); 238 }) 239 240 return data; 241 }, 242 243 extractJomsData: function(jaxData, handler) 244 { 245 var jomsData = []; 246 joms.jQuery.each(eval(jaxData), function() 247 { 248 if (this[0]=='cs' && this[1]==handler) 249 { 250 jomsData.push(this.slice(3)[0]); 251 } 252 }); 253 254 return jomsData; 255 } 256 } 257});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.