|
| 1 | +<big><h1 align="center">vue-async-computed</h1></big> |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <a href="https://npmjs.org/package/vue-async-computed"> |
| 5 | + <img src="https://img.shields.io/npm/v/vue-async-computed.svg?style=flat-square" |
| 6 | + alt="NPM Version"> |
| 7 | + </a> |
| 8 | + |
| 9 | + <a href="https://coveralls.io/r/foxbenjaminfox/vue-async-computed"> |
| 10 | + <img src="https://img.shields.io/coveralls/foxbenjaminfox/vue-async-computed.svg?style=flat-square" |
| 11 | + alt="Coverage Status"> |
| 12 | + </a> |
| 13 | + |
| 14 | + <a href="https://travis-ci.org/foxbenjaminfox/vue-async-computed"> |
| 15 | + <img src="https://img.shields.io/travis/foxbenjaminfox/vue-async-computed.svg?style=flat-square" |
| 16 | + alt="Build Status"> |
| 17 | + </a> |
| 18 | + |
| 19 | + <a href="https://npmjs.org/package/vue-async-computed"> |
| 20 | + <img src="http://img.shields.io/npm/dm/vue-async-computed.svg?style=flat-square" |
| 21 | + alt="Downloads"> |
| 22 | + </a> |
| 23 | + |
| 24 | + <a href="https://david-dm.org/foxbenjaminfox/vue-async-computed.svg"> |
| 25 | + <img src="https://david-dm.org/foxbenjaminfox/vue-async-computed.svg?style=flat-square" |
| 26 | + alt="Dependency Status"> |
| 27 | + </a> |
| 28 | + |
| 29 | + <a href="https://github.com/foxbenjaminfox/vue-async-computed/blob/master/LICENSE"> |
| 30 | + <img src="https://img.shields.io/npm/l/vue-async-computed.svg?style=flat-square" |
| 31 | + alt="License"> |
| 32 | + </a> |
| 33 | +</p> |
| 34 | + |
| 35 | +With this plugin, you can have have computed properties in Vue that are computed asynchronously. |
| 36 | + |
| 37 | +Without using this plugin, you can't do this: |
| 38 | + |
| 39 | +````js |
| 40 | +new Vue({ |
| 41 | + data: { |
| 42 | + userId: 1 |
| 43 | + }, |
| 44 | + computed: { |
| 45 | + username: { |
| 46 | + // Using vue-resource |
| 47 | + return Vue.http.get('/get-username-by-id', { id: this.userId }) |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | +```` |
| 52 | + |
| 53 | +Or rather, you could, but it wouldn't do what you'd want it to do. But using this plugin, it works just like you'd expect: |
| 54 | +
|
| 55 | +````js |
| 56 | +new Vue({ |
| 57 | + data: { |
| 58 | + userId: 1 |
| 59 | + }, |
| 60 | + asyncComputed: { |
| 61 | + username: { |
| 62 | + return Vue.http.get('/get-username-by-id', { id: this.userId }) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | +```` |
| 67 | +
|
| 68 | +This is especily useful with ES7 async functions: |
| 69 | +
|
| 70 | +````js |
| 71 | +new Vue({ |
| 72 | + asyncComputed: { |
| 73 | + async someCalculation () { |
| 74 | + const x = await someAsycFunction() |
| 75 | + const y = await anotherAsyncFunction() |
| 76 | + return x + y |
| 77 | + } |
| 78 | + } |
| 79 | +}) |
| 80 | +```` |
| 81 | +
|
| 82 | +## Install |
| 83 | +
|
| 84 | +````sh |
| 85 | +npm install --save vue-async-computed |
| 86 | +```` |
| 87 | +
|
| 88 | +## Usage example |
| 89 | +
|
| 90 | +````js |
| 91 | +import AsyncComputed from 'vue-async-computed' |
| 92 | +
|
| 93 | +/* Initialize the plugin */ |
| 94 | +Vue.use(AsyncComputed) |
| 95 | +
|
| 96 | +/* Then, when you create a Vue instance (or component), |
| 97 | + you can pass an object named "asyncComputed" as well as |
| 98 | + or instead of one named "computed". The functions you pass |
| 99 | + to "asyncComputed" should return promises, and the values |
| 100 | + those promises resolve to are then asynchronously bound to |
| 101 | + the Vue instance as the promises resolve. Just like with |
| 102 | + normal computed properties, if the data the property depends |
| 103 | + on changes then the property is re-run automatically. |
| 104 | + |
| 105 | + You can almost completely ignore the fact that behind the |
| 106 | + scenes they are asynchronous. The one thing to remember is |
| 107 | + that until a asynchronously property's promise resolves |
| 108 | + for the first time, the value of the computed property is null. |
| 109 | +*/ |
| 110 | + |
| 111 | +const vm = new Vue({ |
| 112 | + data: { |
| 113 | + x: 2, |
| 114 | + y: 3 |
| 115 | + }, |
| 116 | + asyncComputed: { |
| 117 | + sum () { |
| 118 | + const sum = this.x + this.y |
| 119 | + return new Promise(resolve => |
| 120 | + setTimeout(() => resolve(sum), 1000) |
| 121 | + ) |
| 122 | + } |
| 123 | + } |
| 124 | +}) |
| 125 | + |
| 126 | +/* Until one second has passed, vm.sum will be null. |
| 127 | + After that, vm.sum will be 5. If you change vm.x or vm.y, |
| 128 | + one second later vm.sum will automatically update itself to be |
| 129 | + the sum of what you set vm.x and vm.y to be a second before. |
| 130 | +*/ |
| 131 | +```` |
| 132 | + |
| 133 | +## License |
| 134 | + |
| 135 | +MIT © [Benjamin Fox](http://github.com/foxbenjaminfox) |
| 136 | + |
| 137 | +[npm-url]: https://npmjs.org/package/vue-async-computed |
| 138 | +[npm-image]: https://img.shields.io/npm/v/vue-async-computed.svg?style=flat-square |
| 139 | + |
| 140 | +[travis-url]: https://travis-ci.org/foxbenjaminfox/vue-async-computed |
| 141 | +[travis-image]: https://img.shields.io/travis/foxbenjaminfox/vue-async-computed.svg?style=flat-square |
| 142 | + |
| 143 | +[coveralls-url]: https://coveralls.io/r/foxbenjaminfox/vue-async-computed |
| 144 | +[coveralls-image]: https://img.shields.io/coveralls/foxbenjaminfox/vue-async-computed.svg?style=flat-square |
| 145 | + |
| 146 | +[depstat-url]: https://david-dm.org/foxbenjaminfox/vue-async-computed |
| 147 | +[depstat-image]: https://david-dm.org/foxbenjaminfox/vue-async-computed.svg?style=flat-square |
| 148 | + |
| 149 | +[download-badge]: http://img.shields.io/npm/dm/vue-async-computed.svg?style=flat-square |
0 commit comments