Permalink
Browse files

don't use ternary; use `return` in imgLoaded

  • Loading branch information...
1 parent 77ca831 commit bd70f1dab9043f3063a7bb39eb3d0fb94602b6c3 @desandro committed Jan 17, 2012
Showing with 28 additions and 10 deletions.
  1. +28 −10 jquery.imagesloaded.js
View
@@ -34,19 +34,37 @@ $.fn.imagesLoaded = function( callback ) {
broken = [];
function doneLoading() {
- !broken.length ? deferred.resolve( $images ) : deferred.reject( $images, $(proper), $(broken) );
- callback.call( $this, $images, $(proper), $(broken) );
+ var $proper = $(proper),
+ $broken = $(broken);
+
+ if ( broken.length ) {
+ deferred.reject( $images, $proper, $broken );
+ } else {
+ deferred.resolve( $images );
+ }
+
+ callback.call( $this, $images, $proper, $broken );
}
function imgLoaded( event ) {
- if ( event.target.src !== blank && $.inArray( this, loaded ) === -1 ) {
- loaded.push(this);
- event.type == 'error' ? broken.push(this) : proper.push(this);
- deferred.notify( $images.length, loaded.length, proper.length, broken.length );
- if ( --len <= 0 ){
- setTimeout( doneLoading );
- $images.unbind( '.imagesLoaded', imgLoaded );
- }
+ // dont proceed if img src is blank or if img is already loaded
+ if ( event.target.src === blank || $.inArray( this, loaded ) !== -1 ) {
+ return;
+ }
+
+ loaded.push( this );
+ // keep track of broken and properly loaded images
+ if ( event.type === 'error' ) {
+ broken.push( this );
+ } else {
+ proper.push( this );
+ }
+
+ deferred.notify( $images.length, loaded.length, proper.length, broken.length );
+
+ if ( --len <= 0 ){
+ setTimeout( doneLoading );
+ $images.unbind( '.imagesLoaded', imgLoaded );
}
}

2 comments on commit bd70f1d

Collaborator

darsain replied Jan 17, 2012

Just out of curiosity, and drive to become a better dev :) - what was the problem with ternary operators? Are they slower, or is it only readability?

Owner

desandro replied Jan 17, 2012

It was a readability choice. Just a personal style choice.

I like ternaries for assignments, but if I'm just doing a conditional, I'd stick to if. Then if & when you need to add more logic into that conditional, you don't need to break up the ternary.

var foo = condition ? bar : 'baz';

Personally, I'm not a fan of one-line-iness and terse-itude. It hurts readability.

Please sign in to comment.