From 53330304c70f8d5f4d5774c0233c08f473284717 Mon Sep 17 00:00:00 2001 From: ManjaroOne666 Date: Sun, 13 Jan 2019 21:33:55 +0000 Subject: [PATCH] removed common method loadImage into mixin --- components/GalleryImageViewer.vue | 14 ++------------ mixins/imageLoader.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 mixins/imageLoader.js diff --git a/components/GalleryImageViewer.vue b/components/GalleryImageViewer.vue index 00dd3b9..757834b 100644 --- a/components/GalleryImageViewer.vue +++ b/components/GalleryImageViewer.vue @@ -31,11 +31,13 @@ diff --git a/mixins/imageLoader.js b/mixins/imageLoader.js new file mode 100644 index 0000000..1c118be --- /dev/null +++ b/mixins/imageLoader.js @@ -0,0 +1,16 @@ +export default { + methods: { + loadImage(url) { + return new Promise( (resolve, reject) => { + const img = new Image() + + img.addEventListener('load', e => resolve(img)); + img.addEventListener('error', () => { + reject(new Error(`Failed to load image URL: ${url}`)); + }); + + img.src = url; + }) + } + } +}