From c3cf89525c268db389162aec91dfe50ec455b861 Mon Sep 17 00:00:00 2001 From: Thomas Parisot Date: Thu, 23 Jan 2014 16:40:31 +0000 Subject: [PATCH 1/2] Revealing `availableWidths` bug through tests. --- test/unit/core.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/unit/core.js b/test/unit/core.js index 7f89146..5647379 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -124,14 +124,15 @@ describe('Imager.js', function () { it('can be a function computing a value for you', function (done) { // this example will always compute sizes 8 pixels by 8 pixels - // we need to stub it for now as events are triggered automatically and generates exceptions we can escape - var imgr = new Imager(); + var imgr = new Imager({ + onResize: false, + lazyload: false, + availableWidths: function (image) { + return image.clientWidth - image.clientWidth % 8 + (1 * (image.clientWidth % 8 ? 8 : 0)); + } + }); setTimeout(function () { - imgr.availableWidths = function (image) { - return image.clientWidth - image.clientWidth % 8 + (1 * (image.clientWidth % 8 ? 8 : 0)); - }; - var img = { clientWidth: 320 }; var spy = sandbox.spy(imgr, 'availableWidths'); From 679a2c293500c0e852d28852ba1c4eb958560c93 Mon Sep 17 00:00:00 2001 From: Thomas Parisot Date: Thu, 23 Jan 2014 16:41:50 +0000 Subject: [PATCH 2/2] Proper initialisation of Imager instance `availableWidths` option. - removed `isArray` function - bootstrapping instance variables - array/object paths --- Imager.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/Imager.js b/Imager.js index 849e524..c6522a5 100644 --- a/Imager.js +++ b/Imager.js @@ -2,7 +2,7 @@ 'use strict'; - var defaultWidths, getKeys, isArray, nextTick, addEvent; + var defaultWidths, getKeys, nextTick, addEvent; nextTick = window.requestAnimationFrame || window.mozRequestAnimationFrame || @@ -53,10 +53,6 @@ return keys; }; - isArray = function isArray (object) { - return Object.prototype.toString.call(object) === '[object Array]'; - }; - /* Construct a new Imager instance, passing an optional configuration object. @@ -119,24 +115,25 @@ this.lazyload = opts.hasOwnProperty('lazyload') ? opts.lazyload : false; this.scrolled = false; this.availablePixelRatios = opts.availablePixelRatios || [1, 2]; + this.availableWidths = opts.availableWidths || defaultWidths; + this.widthMap = {}; this.refreshPixelRatio(); - if (opts.availableWidths === undefined) { - opts.availableWidths = defaultWidths; - } - - if (isArray(opts.availableWidths)) { - this.availableWidths = opts.availableWidths; + if (typeof this.availableWidths !== 'function'){ + if (typeof this.availableWidths.length === 'number') { this.widthsMap = Imager.createWidthsMap(this.availableWidths); - } - else { - this.availableWidths = getKeys(opts.availableWidths); - this.widthsMap = opts.availableWidths; - } + } + else { + this.widthsMap = this.availableWidths; + this.availableWidths = getKeys(this.availableWidths); + } - this.availableWidths = this.availableWidths.sort(function (a, b) { + this.availableWidths = this.availableWidths.sort(function (a, b) { return a - b; - }); + }); + } + + if (elements) { this.divs = applyEach(elements, returnDirectValue);