Skip to content

Commit 68bfc66

Browse files
committed
compile container dirs and props in proper context instead of parent
1 parent b1a6458 commit 68bfc66

File tree

7 files changed

+43
-40
lines changed

7 files changed

+43
-40
lines changed

src/compiler/compile.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,16 @@ function linkAndCapture (linker, vm) {
9494
*
9595
* @param {Vue} vm
9696
* @param {Array} dirs
97-
* @param {Vue} [parent]
98-
* @param {Array} [parentDirs]
97+
* @param {Vue} [context]
98+
* @param {Array} [contextDirs]
9999
* @return {Function}
100100
*/
101101

102-
function makeUnlinkFn (vm, dirs, parent, parentDirs) {
102+
function makeUnlinkFn (vm, dirs, context, contextDirs) {
103103
return function unlink (destroying) {
104104
teardownDirs(vm, dirs, destroying)
105-
if (parent && parentDirs) {
106-
teardownDirs(parent, parentDirs)
105+
if (context && contextDirs) {
106+
teardownDirs(context, contextDirs)
107107
}
108108
}
109109
}
@@ -146,15 +146,15 @@ exports.compileAndLinkProps = function (vm, el, props) {
146146
/**
147147
* Compile the root element of an instance.
148148
*
149-
* 1. attrs on parent container (parent scope)
149+
* 1. attrs on context container (context scope)
150150
* 2. attrs on the component template root node, if
151151
* replace:true (child scope)
152152
*
153153
* If this is a block instance, we only need to compile 1.
154154
*
155155
* This function does compile and link at the same time,
156156
* since root linkers can not be reused. It returns the
157-
* unlink function for potential parent directives on the
157+
* unlink function for potential context directives on the
158158
* container.
159159
*
160160
* @param {Vue} vm
@@ -166,7 +166,7 @@ exports.compileAndLinkProps = function (vm, el, props) {
166166
exports.compileAndLinkRoot = function (vm, el, options) {
167167
var containerAttrs = options._containerAttrs
168168
var replacerAttrs = options._replacerAttrs
169-
var parentLinkFn, replacerLinkFn
169+
var contextLinkFn, replacerLinkFn
170170

171171
// only need to compile other attributes for
172172
// non-block instances
@@ -176,7 +176,7 @@ exports.compileAndLinkProps = function (vm, el, props) {
176176
if (options._asComponent) {
177177
// 2. container attributes
178178
if (containerAttrs) {
179-
parentLinkFn = compileDirectives(containerAttrs, options)
179+
contextLinkFn = compileDirectives(containerAttrs, options)
180180
}
181181
if (replacerAttrs) {
182182
// 3. replacer attributes
@@ -188,23 +188,23 @@ exports.compileAndLinkProps = function (vm, el, props) {
188188
}
189189
}
190190

191-
// link parent dirs
192-
var parent = vm.$parent
193-
var parentDirs
194-
if (parent && parentLinkFn) {
195-
parentDirs = linkAndCapture(function () {
196-
parentLinkFn(parent, el)
197-
}, parent)
191+
// link context scope dirs
192+
var context = vm._context
193+
var contextDirs
194+
if (context && contextLinkFn) {
195+
contextDirs = linkAndCapture(function () {
196+
contextLinkFn(context, el)
197+
}, context)
198198
}
199199

200200
// link self
201201
var selfDirs = linkAndCapture(function () {
202202
if (replacerLinkFn) replacerLinkFn(vm, el)
203203
}, vm)
204204

205-
// return the unlink function that tearsdown parent
205+
// return the unlink function that tearsdown context
206206
// container directives.
207-
return makeUnlinkFn(vm, selfDirs, parent, parentDirs)
207+
return makeUnlinkFn(vm, selfDirs, context, contextDirs)
208208
}
209209

210210
/**
@@ -522,10 +522,10 @@ function makePropsLinkFn (props) {
522522
vm._data[path] = undefined
523523
} else if (prop.dynamic) {
524524
// dynamic prop
525-
if (vm.$parent) {
525+
if (vm._context) {
526526
if (prop.mode === propBindingModes.ONE_TIME) {
527527
// one time binding
528-
value = vm.$parent.$get(prop.parentPath)
528+
value = vm._context.$get(prop.parentPath)
529529
if (_.assertProp(prop, value)) {
530530
vm[path] = vm._data[path] = value
531531
}

src/directives/component.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ module.exports = {
178178
_linkerCachable: !this.template,
179179
_asComponent: true,
180180
_isRouterView: this._isRouterView,
181-
_contentOwner: this.vm
181+
_context: this.vm
182182
}, this.Ctor)
183183
if (this.keepAlive) {
184184
this.cache[this.ctorId] = child

src/directives/prop.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
bind: function () {
1313

1414
var child = this.vm
15-
var parent = child.$parent
15+
var parent = child._context
1616
// passed in from compiler directly
1717
var prop = this._descriptor
1818
var childKey = prop.path

src/directives/repeat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ module.exports = {
365365
// identifier, shows that this vm belongs to this collection
366366
_repeatId: this.id,
367367
// transclusion content owner
368-
_contentOwner: this.vm
368+
_context: this.vm
369369
}, Ctor)
370370
// cache instance
371371
if (needCache) {

src/element-directives/content.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ module.exports = {
1010
bind: function () {
1111
var vm = this.vm
1212
var host = vm
13-
// we need find the content owner, which is the closest
14-
// non-inline-repeater instance.
13+
// we need find the content context, which is the
14+
// closest non-inline-repeater instance.
1515
while (host.$options._repeat) {
1616
host = host.$parent
1717
}
@@ -21,17 +21,15 @@ module.exports = {
2121
this.fallback()
2222
return
2323
}
24-
var owner =
25-
host.$options._contentOwner ||
26-
host.$parent
24+
var context = host._context
2725
var selector = this.el.getAttribute('select')
2826
if (!selector) {
2927
// Default content
3028
var self = this
3129
var compileDefaultContent = function () {
3230
self.compile(
3331
extractFragment(raw.childNodes, raw, true),
34-
owner,
32+
context,
3533
vm
3634
)
3735
}
@@ -51,7 +49,7 @@ module.exports = {
5149
if (nodes.length) {
5250
content = extractFragment(nodes, raw)
5351
if (content.hasChildNodes()) {
54-
this.compile(content, owner, vm)
52+
this.compile(content, context, vm)
5553
} else {
5654
this.fallback()
5755
}
@@ -65,9 +63,9 @@ module.exports = {
6563
this.compile(_.extractContent(this.el, true), this.vm)
6664
},
6765

68-
compile: function (content, owner, host) {
69-
if (content && owner) {
70-
this.unlink = owner.$compile(content, host)
66+
compile: function (content, context, host) {
67+
if (content && context) {
68+
this.unlink = context.$compile(content, host)
7169
}
7270
if (content) {
7371
_.replace(this.el, content)

src/instance/init.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ exports._init = function (options) {
1818
this.$el = null
1919
this.$parent = options._parent
2020
this.$root = options._root || this
21+
this.$children = []
2122
this.$ = {} // child vm references
2223
this.$$ = {} // element references
2324
this._watchers = [] // all watchers as an array
2425
this._directives = [] // all directives
26+
this._childCtors = {} // inherit:true constructors
2527

2628
// a flag to avoid this being observed
2729
this._isVue = true
@@ -44,9 +46,12 @@ exports._init = function (options) {
4446
this._isBeingDestroyed = false
4547
this._unlinkFn = null
4648

47-
// children
48-
this.$children = []
49-
this._childCtors = {}
49+
// context: the scope in which the component was used,
50+
// and the scope in which props and contents of this
51+
// instance should be compiled in.
52+
this._context =
53+
options._context ||
54+
options._parent
5055

5156
// push self into parent / transclusion host
5257
if (this.$parent) {

test/unit/specs/compiler/compile_spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ if (_.inBrowser) {
3030
$interpolate: function (value) {
3131
return data[value]
3232
},
33-
$parent: {
33+
_context: {
3434
_directives: [],
3535
$get: function (v) {
3636
return 'from parent: ' + v
@@ -220,8 +220,8 @@ if (_.inBrowser) {
220220

221221
it('props on root instance', function () {
222222
// temporarily remove vm.$parent
223-
var parent = vm.$parent
224-
vm.$parent = null
223+
var context = vm._context
224+
vm._context = null
225225
var def = Vue.options.directives._prop
226226
el.setAttribute('a', 'hi')
227227
el.setAttribute('b', '{{hi}}')
@@ -230,7 +230,7 @@ if (_.inBrowser) {
230230
expect(vm._data.a).toBe('hi')
231231
expect(hasWarned(_, 'Cannot bind dynamic prop on a root')).toBe(true)
232232
// restore parent mock
233-
vm.$parent = parent
233+
vm._context = context
234234
})
235235

236236
it('DocumentFragment', function () {

0 commit comments

Comments
 (0)