diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bff8e1b --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +node_modules +coverage +npm-debug.log +yarn-error.log +.nyc_output +coverage.lcov +dist diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8bb2085 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2018 Eduardo San Martin Morote + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..144a665 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +vue-promise-blocks +=== + +Transform your Promises into components + +--- + +WIP + +Lookup an example in the `example` folder diff --git a/example/App.vue b/example/App.vue new file mode 100644 index 0000000..6adc40f --- /dev/null +++ b/example/App.vue @@ -0,0 +1,47 @@ + + + diff --git a/example/index.js b/example/index.js new file mode 100644 index 0000000..c3d8156 --- /dev/null +++ b/example/index.js @@ -0,0 +1,7 @@ +import Vue from 'vue'; +import App from './App'; + +new Vue({ + el: '#app', + render: h => h(App), +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..b816bf1 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "vue-promise-blocks", + "version": "0.0.1", + "description": "Promises as components", + "main": "dist/vue-promise-blocks.cjs.js", + "module": "dist/vue-promise-blocks.es.js", + "unpkg": "dist/vue-promise-blocks.js", + "browser": "dist/vue-promise-blocks.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "prepublishOnly": "rollit" + }, + "files": ["src", "dist", "LICENSE", "README.md"], + "author": "Eduardo San Martin Morote ", + "bugs": { + "url": "https://github.com/posva/vue-promise-blocks/issues" + }, + "homepage": "https://github.com/posva/vue-promise-blocks#readme", + "repository": { + "type": "git", + "url": "git+https://github.com/posva/vue-promise-blocks.git" + }, + "license": "MIT" +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..fb33301 --- /dev/null +++ b/src/index.js @@ -0,0 +1,66 @@ +export default { + name: 'PromiseBlock', + props: { + promise: Promise, + promises: Array, + }, + + data: () => ({ + resolved: false, + data: null, + error: null, + }), + + render(h) { + if (this.error instanceof Error || (this.error && this.error.length)) { + return this.$scopedSlots.error(this.error); + } else if (this.resolved) { + return this.$scopedSlots.default(this.data); + } else { + return this.$slots.default[0]; + } + }, + + watch: { + promise: { + handler(promise) { + if (!promise) return; + this.resolved = false; + this.error = null; + promise + .then(data => { + if (this.promise === promise) { + this.resolved = true; + this.data = data; + } + }) + .catch(err => { + if (this.promise === promise) this.error = err; + }); + }, + immediate: true, + }, + + promises: { + handler(promises) { + if (!promises) return; + this.resolved = false; + this.error = []; + this.data = []; + promises.forEach(p => { + p + .then(data => { + if (this.promises === promises) { + this.resolved = true; + this.data.push(data); + } + }) + .catch(err => { + if (this.promises === promises) this.error.push(err); + }); + }); + }, + immediate: true, + }, + }, +};