Skip to content

Instantly share code, notes, and snippets.

@Skateside
Last active August 29, 2015 14:20
Show Gist options
  • Save Skateside/16577aba381625b15a72 to your computer and use it in GitHub Desktop.
Save Skateside/16577aba381625b15a72 to your computer and use it in GitHub Desktop.
Convert images of a given element into promises and trigger an event when they all load.
/*
MIT license.
Copyright (c) 2015 James "Skateside" Long
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE
*/
/**
* This jQuery plugin creates promises for all images and triggers an
* "imagesloaded" event when the child images have loaded. To use it, bind the
* event then activate the checking:
*
* $('.selector').
* on('imagesloaded', function (e) {
* // function
* }).
* imagePromise();
*
* The [[jQuery.fn.imagePromise]] plugin and [[jQuery.ImagePromise]] class are
* added to jQuery's namespace - the [[jQuery.fn.imagePromise]] creates a `new`
* [[jQuery.ImagePromise]] instance, passing in the element matching the jQuery
* object. This allows simple modification, if needed.
**/
(function (factory) {
// AMD. Register as an anonymous module.
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
// Node/CommonJS style for Browserify
} else if (typeof exports === 'object') {
module.exports = factory(require('jquery'));
// Browser globals
} else {
factory(jQuery);
}
}(function ($) {
'use strict';
/**
* class jQuery.ImagePromise
*
* Creates the promises for the images and triggers an event at the correct
* time.
**/
var ImagePromise = function () {
return this.init.apply(this, arguments);
};
ImagePromise.prototype = {
// JavaScript contructor hack.
constructor: ImagePromise,
/**
* new jQuery.ImagePromise(elem)
* - elem (Element): Element whose children should be converted.
**/
init: function (elem) {
var that = this,
promises = $('img', elem).map(function () {
return that.makePromise(this);
}).toArray();
that.when(promises, function () {
$(elem).trigger('imagesloaded');
});
},
/**
* jQuery.ImagePromise#makePromise(image) -> jQuery.Deferred
* - image (Element): Image element to convert.
*
* This method converts the image itself into a promise. The promise is
* resolved when the image loads (or resolves automatically if the
* image is already resolved) and rejects when the image fails.
**/
makePromise: function (image) {
return $.Deferred(function (deferred) {
if (!image || image.complete) {
deferred.resolve();
} else {
$(image).on({
load: function () {
deferred.resolve();
},
error: function () {
deferred.reject();
}
});
}
}).promise();
},
/**
* jQuery.ImagePromise#when(promises, callback)
* - promises (Array): List of promises.
* - callback (Function): Function to execute when promises have
* resolved.
*
* A simple wrapper for `jQuery.when()` that allows any number of
* promises to be passed rather than having to know the promises
* ahead-of-time.
**/
when: function (promises, callback) {
$.when.apply($, promises).then(callback);
}
};
// Expose the ImagePromise constructor.
$.ImagePromise = ImagePromise;
/**
* jQuery.fn.imagePromise() -> jQuery
*
* jQuery method for activating the promise. This should be called after
* binding to the "imagesloaded" event.
**/
$.fn.imagePromise = function () {
return this.each(function () {
return new $.ImagePromise(this);
});
};
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment