File tree 5 files changed +54
-13
lines changed
dir
8000
ectives/internal 5 files changed +54
-13
lines changed Original file line number Diff line number Diff line change @@ -132,6 +132,10 @@ module.exports = {
132
132
self . transition ( newComponent , cb )
133
133
} )
134
134
} else {
135
+ // update ref for kept-alive component
136
+ if ( cached ) {
137
+ newComponent . _updateRef ( )
138
+ }
135
139
this . transition ( newComponent , cb )
136
140
}
137
141
} ,
@@ -241,6 +245,10 @@ module.exports = {
241
245
}
242
246
var child = this . childVM
243
247
if ( ! child || this . keepAlive ) {
248
+ if ( child ) {
249
+ // remove ref
250
+ child . _updateRef ( true )
251
+ }
244
252
return
245
253
}
246
254
// the sole purpose of `deferCleanup` is so that we can
Original file line number Diff line number Diff line change @@ -79,18 +79,16 @@ exports._init = function (options) {
79
79
this . $parent . $children . push ( this )
80
80
}
81
81
82
- // set ref
83
- if ( options . _ref ) {
84
- ( this . _scope || this . _context ) . $refs [ options . _ref ] = this
85
- }
86
-
87
82
// merge options.
88
83
options = this . $options = mergeOptions (
89
84
this . constructor . options ,
90
85
options ,
91
86
this
92
87
)
93
88
89
+ // set ref
90
+ this . _updateRef ( )
91
+
94
92
// initialize data as empty object.
95
93
// it will be filled up in _initScope().
96
94
this . _data = { }
Original file line number Diff line number Diff line change @@ -2,6 +2,26 @@ var _ = require('../util')
2
2
var Directive = require ( '../directive' )
3
3
var compiler = require ( '../compiler' )
4
4
5
+ /**
6
+ * Update v-ref for component.
7
+ *
8
+ * @param {Boolean } remove
9
+ */
10
+
11
+ exports . _updateRef = function ( remove ) {
12
+ var ref = this . $options . _ref
13
+ if ( ref ) {
14
+ var refs = ( this . _scope || this . _context ) . $refs
15
+ if ( remove ) {
16
+ if ( refs [ ref ] === this ) {
17
+ refs [ ref ] = null
18
+ }
19
+ } else {
20
+ refs [ ref ] = this
21
+ }
22
+ }
23
+ }
24
+
5
25
/**
6
26
* Transclude, compile and link element.
7
27
*
@@ -136,14 +156,8 @@ exports._destroy = function (remove, deferCleanup) {
136
156
var parent = this . $parent
137
157
if ( parent && ! parent . _isBeingDestroyed ) {
138
158
parent . $children . $remove ( this )
139
- // unregister ref
140
- var ref = this . $options . _ref
141
- if ( ref ) {
142
- var scope = this . _scope || this . _context
143
- if ( scope . $refs [ ref ] === this ) {
144
- scope . $refs [ ref ] = null
145
- }
146
- }
159
+ // unregister ref (remove: true)
160
+ this . _updateRef ( true )
147
161
}
148
162
// destroy all children.
149
163
i = this . $children . length
Original file line number Diff line number Diff line change @@ -53,6 +53,25 @@ if (_.inBrowser) {
53
53
} )
54
54
} )
55
55
56
+ it ( 'with dynamic component + keep-alive' , function ( done ) {
57
+ var vm = new Vue( {
58
+ el : el ,
59
+ components : components ,
60
+ data : { test : 'test' } ,
61
+ template : '<component :is="test" v-ref:test keep-alive></component>'
62
+ } )
63
+ expect ( vm . $refs . test . $options . id ) . toBe ( 'test' )
64
+ vm . test = 'test2'
65
+ _ . nextTick ( function ( ) {
66
+ expect ( vm . $refs . test . $options . id ) . toBe ( 'test2' )
67
+ vm . test = ''
68
+ _ . nextTick ( function ( ) {
69
+ expect ( vm . $refs . test ) . toBe ( null )
70
+ done ( )
71
+ } )
72
+ } )
73
+ } )
74
+
56
75
it ( 'should be reactive when bound by dynamic component and hoisted' , function ( done ) {
57
76
var vm = new Vue ( {
58
77
el : el ,
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ describe('Instance Init', function () {
6
6
constructor : {
7
7
options : { a : 1 , b : 2 }
8
8
} ,
9
+ _updateRef : jasmine . createSpy ( ) ,
9
10
_initEvents : jasmine . createSpy ( ) ,
10
11
_callHook : jasmine . createSpy ( ) ,
11
12
_initState : jasmine . createSpy ( ) ,
@@ -38,6 +39,7 @@ describe('Instance Init', function () {
38
39
it ( 'should call other init methods' , function ( ) {
39
40
expect ( stub . _initEvents ) . toHaveBeenCalled ( )
40
41
expect ( stub . _initState ) . toHaveBeenCalled ( )
42
+ expect ( stub . _updateRef ) . toHaveBeenCalled ( )
41
43
} )
42
44
43
45
it ( 'should call created hook' , function ( ) {
You can’t perform that action at this time.
0 commit comments