Changeset 2635 for lang/javascript/jsdeferred
- Timestamp:
- 12/06/07 17:18:28 (13 months ago)
- Location:
- lang/javascript/jsdeferred/trunk
- Files:
-
- 2 added
- 3 modified
-
binding (added)
-
binding/jquery.js (added)
-
jsdeferred.js (modified) (10 diffs)
-
sample.html (modified) (3 diffs)
-
test.html (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/javascript/jsdeferred/trunk/jsdeferred.js
r2633 r2635 23 23 * THE SOFTWARE. 24 24 */ 25 (function ($) { 26 /* Usage:: 25 /* Usage (with jQuery):: 27 26 * 28 27 * $.deferred.define(); … … 52 51 * 53 52 * Sample: 54 * var d = new $.deferred(); //=> new Deferred;53 * var d = new Deferred(); 55 54 * // or this is shothand of above. 56 * var d = $.deferred();55 * var d = Deferred(); 57 56 */ 58 57 /* function Deferred.prototype.next (fun) //=> Deferred … … 126 125 }; 127 126 128 /* function Deferred.register (name, fun) //=> void 0129 *130 * Register `fun` to Deferred prototype for method chain.131 *132 * Sample::133 * // Deferred.register("loop", loop);134 *135 * // Global Deferred function136 * loop(10, function (n) {137 * print(n);138 * }).139 * // Registered Deferred.prototype.loop140 * loop(10, function (n) {141 * print(n);142 * });143 */144 Deferred.register = function (name, fun) {145 this.prototype[name] = function () {146 return this.next(Deferred.wrap(fun).apply(null, arguments));147 };148 };149 150 /* function Deferred.wrap (dfun) //=> Function151 *152 * Create and return function which run `dfun` with arguments.153 *154 * Sample::155 * var dloop = Deferred.wrap(loop);156 *157 * next(dloop(10, function (n) {158 * print(n);159 * }));160 */161 Deferred.wrap = function (dfun) {162 return function () {163 var a = arguments;164 return function () {165 return dfun.apply(null, a);166 };167 };168 };169 170 127 /* function parallel (deferredlist) //=> Deferred 171 128 * … … 192 149 * }); 193 150 */ 194 function parallel(dl) {151 Deferred.parallel = function (dl) { 195 152 var ret = new Deferred(), values = {}, num = 0; 196 153 for (var i in dl) { … … 226 183 * }); 227 184 */ 228 function wait(n) {185 Deferred.wait = function (n) { 229 186 var d = new Deferred(), t = new Date(); 230 187 var id = setTimeout(function () { … … 235 192 return d; 236 193 } 237 Deferred.register("wait", wait);238 194 239 195 /* function next (fun) //=> Deferred … … 242 198 * is called after current queue. 243 199 */ 244 function next(fun) {200 Deferred.next = function (fun) { 245 201 var d = new Deferred(); 246 202 var id = setTimeout(function () { clearTimeout(id); d.call() }, 0); … … 271 227 * 272 228 */ 273 function call(f, args) {229 Deferred.call = function (f, args) { 274 230 args = Array.prototype.slice.call(arguments); 275 231 f = args.shift(); … … 297 253 * }); 298 254 */ 299 function loop(n, fun) {255 Deferred.loop = function (n, fun) { 300 256 var o = { 301 257 begin : n.begin || 0, … … 330 286 }); 331 287 } 332 Deferred.register("loop", loop); 333 334 $.deferred = Deferred; 335 $.deferred.parallel = parallel; 336 $.deferred.wait = wait; 337 $.deferred.next = next; 338 $.deferred.call = call; 339 $.deferred.loop = loop; 340 $.deferred.define = function (obj, list) { 288 289 /* function Deferred.register (name, fun) //=> void 0 290 * 291 * Register `fun` to Deferred prototype for method chain. 292 * 293 * Sample:: 294 * // Deferred.register("loop", loop); 295 * 296 * // Global Deferred function 297 * loop(10, function (n) { 298 * print(n); 299 * }). 300 * // Registered Deferred.prototype.loop 301 * loop(10, function (n) { 302 * print(n); 303 * }); 304 */ 305 Deferred.register = function (name, fun) { 306 this.prototype[name] = function () { 307 return this.next(Deferred.wrap(fun).apply(null, arguments)); 308 }; 309 }; 310 311 /* function Deferred.wrap (dfun) //=> Function 312 * 313 * Create and return function which run `dfun` with arguments. 314 * 315 * Sample:: 316 * var dloop = Deferred.wrap(loop); 317 * 318 * next(dloop(10, function (n) { 319 * print(n); 320 * })); 321 */ 322 Deferred.wrap = function (dfun) { 323 return function () { 324 var a = arguments; 325 return function () { 326 return dfun.apply(null, a); 327 }; 328 }; 329 }; 330 331 Deferred.register("loop", Deferred.loop); 332 Deferred.register("wait", Deferred.wait); 333 334 Deferred.define = function (obj, list) { 341 335 if (!list) list = ["parallel", "wait", "next", "call", "loop"]; 342 336 if (!obj) obj = (function () { return this })(); 343 $.each(list, function (n, i) { 344 obj[i] = $.deferred[i]; 345 }); 337 for (var i = 0; i < list.length; i++) { 338 var n = list[i]; 339 obj[n] = Deferred[n]; 340 } 346 341 }; 347 342 348 // override jQuery Ajax functions349 $.each(["get", "getJSON", "post"], function (n, i) {350 var orig = $[i];351 $[i] = function (url, data, callback) {352 if (typeof data == "function") {353 data = undefined;354 callback = data;355 }356 var d = $.deferred();357 orig(url, data, function (data) {358 d.call(data);359 });360 if (callback) d = d.next(callback);361 return d;362 };363 });364 365 // end366 })(jQuery); -
lang/javascript/jsdeferred/trunk/sample.html
r2633 r2635 2 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 3 <head> 4 <title> jQueryDeferred Sample</title>4 <title>JSDeferred Sample</title> 5 5 6 6 <script type="text/javascript" src="http://coderepos.org/share/htdocs/lib/jquery-1.2.1.min.js"></script> 7 7 <script type="text/javascript" src="jsdeferred.js"></script> 8 <script type="text/javascript" src="binding/jquery.js"></script> 8 9 <script type="text/javascript"> 9 10 // <![CDATA[ 10 $.deferred.define();11 12 11 13 12 // ]]> … … 57 56 <body> 58 57 <div id="whole"> 59 <h1 id="top"> jQueryDeferred Sample</h1>58 <h1 id="top">JSDeferred Sample</h1> 60 59 <p>About Deferred: <a href="http://mochikit.com/doc/html/MochiKit/Async.html">MochiKit.Async</a></p> 61 60 <p>Deferred is able to write async code cleanly.</p> … … 70 69 <pre> 71 70 // export functions to global; 72 $.deferred.define();71 Deferred.define(); 73 72 74 73 // export to aObj 75 // $.deferred.define(aObj);74 // Deferred.define(aObj); 76 75 77 76 // export specic functions 78 77 // var global = (function () { return this })(); 79 // $.deferred.define(global, ["next"]);78 // Deferred.define(global, ["next"]); 80 79 81 80 // full name 82 $.deferred.next(fun);81 Deferred.next(fun); 83 82 </pre> 84 83 </div> -
lang/javascript/jsdeferred/trunk/test.html
r2633 r2635 2 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 3 <head> 4 <title> jQueryDeferred Test</title>4 <title>JSDeferred Test</title> 5 5 6 6 <script type="text/javascript" src="http://coderepos.org/share/htdocs/lib/jquery-1.2.1.min.js"></script> 7 7 <script type="text/javascript" src="jsdeferred.js"></script> 8 <script type="text/javascript" src="binding/jquery.js"></script> 8 9 <script type="text/javascript" src="test-jsdeferred.js"></script> 9 10 <style type="text/css"> … … 90 91 <body> 91 92 <div id="whole"> 92 <h1 id="top"> jQueryDeferred Test <span id="nums">0/0</span></h1>93 <h1 id="top">JSDeferred Test <span id="nums">0/0</span></h1> 93 94 94 95 <div id="content">
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)