var tmpl, data, count = 2; function decount() { if ( ! --count ) { apply_template( tmpl, data ); } } $.ajax({ url: "template.php", success: function( t ) { tmpl = t; decount(); } }); $.ajax({ url: "getMyId.php", data: { name: "Julian" }, success: function( id ) { $.ajax({ url: "data.php", data: { id: id }, success: function( d ) { data = d; decount(); } }); } });
var tmpl = $.ajax( "template.php" ); $.ajax( "getMyId.php", { data: { name: "Julian" }, success: function( id ) { $.when( tmpl, $.ajax( "data.php", { data: { id: id } }); ).done(function( t, d ) { apply_template( t[ 0 ], d[ 0 ] ); }); } });
$.when( $.ajax( "template.php" ), $.ajax( "getMyId.php", { data: { name: "Julian" } } ).pipe(function( id ) { return $.ajax( "data.php", { data: { id: id } }); }) ).done(function( t, d ) { apply_template( t[ 0 ], d[ 0 ] ); });
$.when( $.ajax( "template.php" ), $.ajax( "getMyId.php", { data: { name: "Julian" } } ).pipe(function( id ) { return $.ajax( "data.php", { data: { id: id } }); }) ).done(function( t, d ) { apply_template( t[ 0 ], d[ 0 ] ); });
var defer = $.Deferred(); var filtered = defer.pipe(function( company ) { return company + " is always late"; }); filtered.done( output ); defer.resolve( "American Airlines" );
var defer = $.Deferred(); var filtered = defer.pipe(function( company ) { return $.Deferred(function( defer ) { setTimeout( function() { defer.resolve( company + " is still late" ); }, 1000 ); }); }); filtered.done( output ); defer.resolve( "American Airlines" );
var defer = $.Deferred(); var filtered = defer.pipe(function( company ) { return $.Deferred(function( defer ) { setTimeout( function() { defer.reject( company + " is late, damnit!" ); }, 1000 ); }); }); filtered.fail( output ); defer.resolve( "American Airlines" );
var defer = $.Deferred().done( function() { output( "done!" ); }).progress( output ); defer.notify( "hello" ); defer.notify( "world" ); defer.progress(function( data ) { output( "other handler: " + data ); }); defer.notify( "two" ); defer.resolve(); defer.notify( "donotwant" ); defer.progress( output );
var defer = $.Deferred().then( null, null, output ); defer.notify( "hello" ); defer.notify( "world" );
var defer = $.Deferred().progress( output ); var halfDone = defer.pipe( null, null, function( value ) { return value == 50 ? $.Deferred().resolve() : value ; }); halfDone.progress(function( value ) { output( "half done: " + value ); }).done(function() { output( "half done!" ); }); defer.notify( 0 ); defer.notify( 25 ); defer.notify( 50 ); defer.notify( 75 ); defer.notify( 100 );
var defer1 = $.Deferred(); var defer2 = $.Deferred(); $.when( defer1, defer2 ).progress(function( value1, value2 ) { output( "v1: " + value1 + ", v2: " + value2 ); }); defer1.notify( 0 ); defer2.notify( 50 ); defer1.notify( 100 ); defer2.notify( 100 );
var defer = $.Deferred(); output( defer.isProgressing() ); defer.notify( "see?" ); output( defer.isProgressing() ); defer.resolve(); output( defer.isProgressing() );
var cbs = $.Callbacks(); var fn = function( value ) { output( "fn: " + value ); return false; }; cbs.add( fn ); cbs.fire( "hello" ); cbs.add( output ); cbs.fire( "world" ); cbs.remove( output ); cbs.fire( "end" );
var cbs = $.Callbacks( "memory" ); var fn = function( value ) { output( "fn: " + value ); return false; }; cbs.add( fn ); cbs.fire( "hello" ); cbs.add( output ); cbs.fire( "world" ); cbs.remove( output ); cbs.fire( "end" );
var cbs = $.Callbacks( "once" ); var fn = function( value ) { output( "fn: " + value ); return false; }; cbs.add( fn ); cbs.fire( "hello" ); cbs.add( output ); cbs.fire( "world" ); cbs.remove( output ); cbs.fire( "end" );
var cbs = $.Callbacks( "stopOnFalse" ); var fn = function( value ) { output( "fn: " + value ); return false; }; cbs.add( fn ); cbs.fire( "hello" ); cbs.add( output ); cbs.fire( "world" ); cbs.remove( output ); cbs.fire( "end" );
var cbs = $.Callbacks( "once memory" ); var fn = function( value ) { output( "fn: " + value ); return false; }; cbs.add( fn ); cbs.fire( "hello" ); cbs.add( output ); cbs.fire( "world" ); cbs.remove( output ); cbs.fire( "end" );
var cb1 = $.Callbacks( "once memory" ); var cb2 = $.Callbacks(); cb1.add( cb2.fire ); cb1.fire( "DUNNO WHAT I R DOIN!" );
var defer = $.Deferred(); var cb2 = $.Callbacks(); defer.done( cb2.fire ); defer.resolve( "OH! ME CAN HALF READ NOW!" );
var cbs = $.Callbacks(); var add = cbs.add; var fire = cbs.fire; add( output ); fire( "see?" );
var topics = {}; jQuery.Topic = function( id ) { var callbacks, method, topic = id && topics[ id ]; if ( !topic ) { callbacks = jQuery.Callbacks(); topic = { publish: callbacks.fire, subscribe: callbacks.add, unsubscribe: callbacks.remove }; if ( id ) { topics[ id ] = topic; } } return topic; };
$.Topic( "myTopic" ).subscribe( output ); // Then in another module far far away $.Topic( "myTopic" ).publish( "hihihi" );
var cb1 = $.Callbacks( "once memory" ); var cb2 = $.Callbacks(); cb1.add( cb2.fire ); cb1.fire( "DUNNO WHAT I R DOIN!" );
var defer = $.Deferred(); var topic = $.Topic( "myGlobalTopic" ); defer.done( topic.publish ); defer.resolve( "OMG! PUBLISH!" );