diff --git a/addons/web_unsplash/static/src/js/unsplash_image_widget.js b/addons/web_unsplash/static/src/js/unsplash_image_widget.js index 61a698ed190..01785fc12d9 100644 --- a/addons/web_unsplash/static/src/js/unsplash_image_widget.js +++ b/addons/web_unsplash/static/src/js/unsplash_image_widget.js @@ -112,18 +112,20 @@ widgetsMedia.ImageWidget.include({ } this._unsplash.query = needle; + + var always = function () { + if (!noRender) { + self._renderImages(); + self._adaptLoadMore(); + } + }; return this.unsplashAPI.getImages(needle, this.IMAGES_DISPLAYED_TOTAL).then(function (res) { self._unsplash.isMaxed = res.isMaxed; self._unsplash.records = res.images; self._unsplash.error = false; }, function (err) { self._unsplash.error = err; - }).always(function () { - if (!noRender) { - self._renderImages(); - self._adaptLoadMore(); - } - }); + }).then(always).guardedCatch(always); }, //-------------------------------------------------------------------------- diff --git a/addons/web_unsplash/static/src/js/unsplashapi.js b/addons/web_unsplash/static/src/js/unsplashapi.js index 3055ec306e8..8156be6c4bc 100644 --- a/addons/web_unsplash/static/src/js/unsplashapi.js +++ b/addons/web_unsplash/static/src/js/unsplashapi.js @@ -27,17 +27,17 @@ var UnsplashCore = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, { * * @param {String} query search terms * @param {Integer} pageSize number of image to display per page + * @returns {Promise} */ getImages: function (query, pageSize) { - var self = this; var from = 0; var to = pageSize; var cachedData = this._cache[query]; if (cachedData && (cachedData.images.length >= to || (cachedData.totalImages !== 0 && cachedData.totalImages < to))) { - return $.when({ images: cachedData.images.slice(from, to), isMaxed: to > cachedData.totalImages }); + return Promise.resolve({ images: cachedData.images.slice(from, to), isMaxed: to > cachedData.totalImages }); } - return self._fetchImages(query).then(function (cachedData) { + return this._fetchImages(query).then(function (cachedData) { return { images: cachedData.images.slice(from, to), isMaxed: to > cachedData.totalImages }; }); }, @@ -50,6 +50,7 @@ var UnsplashCore = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, { * Fetches images from unsplash and stores it in cache * * @param {String} query search terms + * @returns {Promise} * @private */ _fetchImages: function (query) { @@ -72,7 +73,7 @@ var UnsplashCore = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, { params: payload, }).then(function (result) { if (result.error) { - return $.Deferred().reject(result.error); + return Promise.reject(result.error); } cachedData.pageCached++; cachedData.images.push.apply(cachedData.images, result.results);