File tree 4 files changed +72
-6
lines changed
4 files changed +72
-6
lines changed Original file line number Diff line number Diff line change 2
2
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
3
3
# Changelog
4
4
5
+ - [ v3.0.0] ( #v300 )
5
6
- [ v2.1.1] ( #v211 )
6
7
- [ v2.1.0] ( #v210 )
7
8
- [ v2.0.0] ( #v200 )
12
13
13
14
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
14
15
16
+ ### v3.0.0
17
+ * Pass the raw error to the error handler when passed the ` useRawError ` option.
18
+ * Allow default values to be given as functions.
19
+
15
20
### v2.1.1
16
21
* Automatic installation when used in a script tag.
17
22
Original file line number Diff line number Diff line change @@ -200,6 +200,28 @@ new Vue({
200
200
*/
201
201
` ` ` `
202
202
203
+ You can instead define the default value as a function , in order to depend on
204
+ props or on data:
205
+
206
+ ````js
207
+ new Vue ({
208
+ data: {
209
+ postId: 1
210
+ },
211
+ asyncComputed: {
212
+ blogPostContent: {
213
+ get () {
214
+ return Vue .http .get (' /post/' + this .postId )
215
+ .then (response => response .data .postContent )
216
+ },
217
+ default () {
218
+ return ' Loading post ' + this .postId
219
+ }
220
+ }
221
+ }
222
+ }
223
+ ` ` ` `
224
+
203
225
## Options
204
226
205
227
By default , in case of a rejected promise in an async computed property , vue - async - computed will take care of logging the error for you .
Original file line number Diff line number Diff line change @@ -11,17 +11,14 @@ const AsyncComputed = {
11
11
Vue . mixin ( {
12
12
beforeCreate ( ) {
13
13
const optionData = this . $options . data
14
- const newData = { }
15
14
16
15
if ( ! this . $options . computed ) this . $options . computed = { }
17
16
18
17
Object . keys ( this . $options . asyncComputed || { } ) . forEach ( key => {
19
- const fn = this . $options . asyncComputed [ key ]
20
- const get = typeof fn === 'function' ? fn : fn . get ,
21
- def = typeof fn . default === 'undefined' ? null : fn . default
18
+ const fn = this . $options . asyncComputed [ key ] ,
19
+ get = typeof fn === 'function' ? fn : fn . get
22
20
23
21
this . $options . computed [ prefix + key ] = get
24
- newData [ key ] = def
25
22
} )
26
23
27
24
this . $options . data = function vueAsyncComputedInjectedDataFn ( ) {
@@ -30,11 +27,24 @@ const AsyncComputed = {
30
27
? optionData . call ( this )
31
28
: optionData
32
29
) || { }
33
- Object . keys ( newData ) . forEach ( key => { data [ key ] = newData [ key ] } )
30
+ Object . keys ( this . $options . asyncComputed || { } ) . forEach ( key => {
31
+ data [ key ] = null
32
+ } )
34
33
return data
35
34
}
36
35
} ,
37
36
created ( ) {
37
+ Object . keys ( this . $options . asyncComputed || { } ) . forEach ( key => {
38
+ const fn = this . $options . asyncComputed [ key ] ,
39
+ def = typeof fn . default === 'undefined' ? null : fn . default
40
+
41
+ if ( typeof def === 'function' ) {
42
+ this [ key ] = def . call ( this )
43
+ } else {
44
+ this [ key ] = def
45
+ }
46
+ } )
47
+
38
48
Object . keys ( this . $options . asyncComputed || { } ) . forEach ( key => {
39
49
let promiseId = 0
40
50
this . $watch ( prefix + key , newPromise => {
Original file line number Diff line number Diff line change @@ -231,3 +231,32 @@ test("Async computed values can have defaults", t => {
231
231
t . equal ( vm . z , true , 'z resolves to true' )
232
232
} )
233
233
} )
234
+
235
+ test ( "Default values can be functions" , t => {
236
+ t . plan ( 4 )
237
+ const vm = new Vue ( {
238
+ data : {
239
+ x : 1
240
+ } ,
241
+ asyncComputed : {
242
+ y : {
243
+ default ( ) { return 2 } ,
244
+ get ( ) {
245
+ return Promise . resolve ( 3 )
246
+ }
247
+ } ,
248
+ z : {
249
+ default ( ) { return this . x } ,
250
+ get ( ) {
251
+ return Promise . resolve ( 4 )
252
+ }
253
+ }
254
+ }
255
+ } )
256
+ t . equal ( vm . y , 2 )
257
+ t . equal ( vm . z , 1 )
258
+ Vue . nextTick ( ( ) => {
259
+ t . equal ( vm . y , 3 )
260
+ t . equal ( vm . z , 4 )
261
+ } )
262
+ } )
You can’t perform that action at this time.
0 commit comments