Can I wrap persistence.js call in something so I know when the last async call has finished?

126 views
Skip to first unread message

rmahnovetsky

unread,
Aug 14, 2011, 8:10:32 AM8/14/11
to persistence.js
I'm making multiple calls to the db then rendering the results to a
view. Is there a way I could wrap all the calls in something then I
know when the last async call comes back so I can continue executing
something else...

Jake Mumm

unread,
Aug 14, 2011, 8:24:57 AM8/14/11
to persis...@googlegroups.com

look up "callback counters"

rmahnovetsky

unread,
Aug 14, 2011, 9:30:39 AM8/14/11
to persistence.js
thanks for the tip... I found this based on your suggestion... My GUI
is rendering is beautifully now :).

http://forrst.com/posts/callWhenDone_a_simple_synchronized_callback_clo-zi1

On Aug 14, 10:24 pm, Jake Mumm <grend...@gmail.com> wrote:
> look up "callback counters"

Neville Franks

unread,
Aug 14, 2011, 5:36:27 PM8/14/11
to rmahnovetsky
Glad you found a solution. You might also want to have a look at
jQuery Deffered http://api.jquery.com/category/deferred-object/
http://intridea.com/2011/2/8/fun-with-jquery-deferred?blog=company

I am using that with Persistence.js I've written a function that lets
you wrap normal functions in a jQuery Deffered.

// Wrap a normal function inside a jQuery Deferred.
// @param fdeferred deferred function to call.
// @param params any params needed for the function - optional. Likely a Options Object.
// @note The fdeferred signature is: function( params, oncomplete, onfail ) where
// params is what is passed in here.
// oncomplete is function that the deferred function must call when it finishes successfully.
// oncomplete is function that the deferred function must call when it finishes unsuccessfully.
var wrapDeferred = function( fdeferred, params ){
// Set object as a promise and return same.
// This works with params as first arguement to fdeferred. Must require the: function( dfd ) wrapper!
// The deferred.then() function is passed the 'args' that fdeferred.oncomplete() passes.
return $.Deferred( function( dfd ) {
if ( params )
fdeferred( params,
function( args ){ dfd.resolve( args ) }, // the function that oncomplete() calls. Triggers the deferred.done() or .then() function.
function( args ){ dfd.reject( args ) } // the function that onfail() calls. Triggers the deferred.fail() or .then() function.
// Note: If fdeferred() always succeeds it doesn't need the onfail parameter.
);
else // as above, but no params.
fdeferred( function( args ){ dfd.resolve( args ) },
function( args ){ dfd.reject( args ) }
);

}).promise();
}

Usage:

/** Search for the spec'd App.
*/
function findApp( appName, oncomplete, onfail ){
Application.findBy( 'name', appName, function( err, result ){
if ( err ){
onfail( err );
}
else{
oncomplete( result );
}
});
}


/** Get the AppData (viewmodel etc.) for the spec'd App.
* @note Returns the most recent AppData - will enable older versions to be retrieved in a later release.
* @caller AppBuilderModule.loadApplication().
*/
function getApp( appName, oncomplete, onfail ){
// Find the spec'd App
mSAIGLib1.wrapDeferred( findApp, appName ).done( function( application ){
// If App exists
if ( application != null ){
// Order by 'created'- descending and get first matching row (ie. the newest).
application.appData.order( 'created', false ).one( function( err, appData ){
if ( err )
onfail( err );
else{
var info = { app: application, appData: appData, appAssets: appData.appAssets }; // Pass Application, its AppData and AppAssets.
oncomplete( info );
}
});
}
else // app not found
oncomplete( null );
})
.fail( function( args ){
onfail( args );
});
}

This may be of interest to others.


Sunday, August 14, 2011, 11:30:39 PM, you wrote:

r> thanks for the tip... I found this based on your suggestion... My GUI
r> is rendering is beautifully now :).

r> http://forrst.com/posts/callWhenDone_a_simple_synchronized_callback_clo-zi1

r> On Aug 14, 10:24�pm, Jake Mumm <grend...@gmail.com> wrote:
>> look up "callback counters"
>> On Aug 14, 2011 8:10 AM, "rmahnovetsky" <rmahnovet...@gmail.com> wrote:
>>
>> > I'm making multiple calls to the db then rendering the results to a
>> > view. Is there a way I could wrap all the calls in something then I
>> > know when the last async call comes back so I can continue executing
>> > something else...

--
Best regards,
Neville Franks, Author of Surfulater - Your off-line Digital Reference Library
Soft As It Gets Pty Ltd, http://www.surfulater.com - Download your copy now.
Victoria, Australia Blog: http://blog.surfulater.com

Reply all
Reply to author
Forward
0 new messages