No, it is not exactly how $.each works. Firstly, $.each _is_ the loop so you cannot do setup or cleanup work (to be performed only once) inside the callback (without obtrusive if-statements which get checked every iteration). Secondly, it does not allow the user to _chain_ jQuery functions (as I did in my example). Thirdly, I never said it was asynchronous, I said that _I_ needed to perform asynchronous work (before operating on each node) and I said the .list functions allows the developer to do work which is _possibly_ asynchronous. I even put a comment identifying where content typically found in a .each call would be found.
I understand what $.each (and .each) are, I use them, but they do not allow me to chain my calls succinctly. I am developing real-world software, I had a need for it, and I thought several others might find it valuable.
Regards,
Mark M. Young
P.S. Redacted development code where I found this useful:
- function init_theme( glo )
{
return( $.Deferred( function( deferred )
{
$('input[name="theme"]').on( 'change', function()
{
var $this = $(this);
Entities.LastUsed.all().filter( 'key', '=', $this.attr( 'name' )).limit( 1 ).each( function( lastUsed )
{
lastUsed.value = $this.val();
persistence.add( lastUsed ).flush();
});
}).list( function( domNodes )
{
// This would be an asynchronous part.
Entities.LastUsed.all().filter( 'key', '=', $(this).attr( 'name' )).limit( 1 ).each( function( lastUsed )
{
if( !lastUsed ){persistence.add( lastUsed = new Entities.LastUsed({'key':'theme', 'value':'c'}));}
// I understand this is synchronous just like $.each and .each.
domNodes.forEach( function( domNode )
{
var $domNode = $(domNode);
$domNode.prop( 'checked', ($domNode.val() === lastUsed.value));
});
// This is another asynchronous part.
persistence.flush( function()
{
$('body').switchToTheme( lastUsed.value );
$('input[name="theme"]').checkboxradio( 'refresh' );
deferred.resolve( true );
});
});
}).on( 'change', function()
{
$('body').switchToTheme( $(this).val());
});
}).promise());
}
P.P.S. Very handy use of line highlighting with a hash on GitHub--I didn't know that existed.