partial: fix dynamic partial scope issue (fix #1363)

This commit is contained in:
Evan You 2015-09-27 12:12:56 -04:00
parent ab2ff32a88
commit 3f3b612905
3 changed files with 29 additions and 9 deletions

View File

@ -64,7 +64,6 @@ exports.$delete = function (key) {
* @param {Object} [options]
* - {Boolean} deep
* - {Boolean} immediate
* - {Boolean} user
* @return {Function} - unwatchFn
*/
@ -77,7 +76,6 @@ exports.$watch = function (expOrFn, cb, options) {
}
var watcher = new Watcher(vm, expOrFn, cb, {
deep: options && options.deep,
user: !options || options.user !== false,
filters: parsed && parsed.filters
})
if (options && options.immediate) {

View File

@ -1,6 +1,7 @@
var _ = require('../../util')
var FragmentFactory = require('../../fragment/factory')
var vIf = require('../public/if')
var Watcher = require('../../watcher')
module.exports = {
@ -24,14 +25,16 @@ module.exports = {
setupDynamic: function (exp) {
var self = this
this.unwatch = this.vm.$watch(exp, function (value) {
var onNameChange = function (value) {
vIf.remove.call(self)
self.insert(value)
}, {
immediate: true,
user: false,
if (value) {
self.insert(value)
}
}
this.nameWatcher = new Watcher(this.vm, exp, onNameChange, {
scope: this._scope
})
onNameChange(this.nameWatcher.value)
},
insert: function (id) {
@ -46,7 +49,11 @@ module.exports = {
},
unbind: function () {
if (this.frag) this.frag.destroy()
if (this.unwatch) this.unwatch()
if (this.frag) {
this.frag.destroy()
}
if (this.nameWatcher) {
this.nameWatcher.teardown()
}
}
}

View File

@ -48,6 +48,21 @@ describe('Partial', function () {
})
})
it('dynamic inside v-for', function () {
new Vue({
el: el,
template: '<div v-for="id in list"><partial v-bind:name="\'test-\' + id"></partial></div>',
data: {
list: ['a', 'b']
},
partials: {
'test-a': 'a {{id}}',
'test-b': 'b {{id}}'
}
})
expect(el.textContent).toBe('a ab b')
})
it('caching', function () {
var calls = 0
var compile = compiler.compile