Skip to content

Commit 2d3cf2d

Browse files
committed
do not set up watchers if v-with is bound to literal value (fix vuejs#776 & vuejs#813)
1 parent cee5b57 commit 2d3cf2d

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/directives/with.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var _ = require('../util')
22
var Watcher = require('../watcher')
3+
var expParser = require('../parsers/expression')
4+
var literalRE = /^(true|false|\s?('[^']*'|"[^"]")\s?)$/
35

46
module.exports = {
57

@@ -20,6 +22,17 @@ module.exports = {
2022
_.warn(
2123
'v-with must be used on an instance with a parent.'
2224
)
25+
} else if (literalRE.test(parentKey)) {
26+
// no need to setup watchers for literal bindings
27+
if (!this.arg) {
28+
_.warn(
29+
'v-with cannot bind literal value as $data: ' +
30+
parentKey
31+
)
32+
} else {
33+
var value = expParser.parse(parentKey).get()
34+
child.$set(childKey, value)
35+
}
2336
} else {
2437

2538
// simple lock to avoid circular updates.

test/unit/specs/directives/with_spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,35 @@ if (_.inBrowser) {
152152
expect(el.innerHTML).toBe('<!--v-start--><p>AAA</p><p>DDD</p><!--v-end--><!--v-component-->')
153153
})
154154

155+
it('bind literal values should not trigger setter warning', function (done) {
156+
var vm = new Vue({
157+
el: el,
158+
template: '<div v-component="test" v-with="a:\'test\'"></div>',
159+
components: {
160+
test: {
161+
template: '{{a}}'
162+
}
163+
}
164+
})
165+
expect(el.firstChild.innerHTML).toBe('test')
166+
vm._children[0].a = 'changed'
167+
_.nextTick(function () {
168+
expect(el.firstChild.innerHTML).toBe('changed')
169+
expect(_.warn).not.toHaveBeenCalled()
170+
done()
171+
})
172+
})
173+
174+
it('should warn when binding literal value without childKey', function () {
175+
var vm = new Vue({
176+
el: el,
177+
template: '<div v-component="test" v-with="\'test\'"></div>',
178+
components: {
179+
test: {}
180+
}
181+
})
182+
expect(_.warn).toHaveBeenCalled()
183+
})
184+
155185
})
156186
}

0 commit comments

Comments
 (0)