8000 v-for: support access for parent scope's $refs and $els in for context. · Mat-Moo/vue@48a6c03 · GitHub
[go: up one dir, main page]

Skip to content

Commit 48a6c03

Browse files
committed
v-for: support access for parent scope's $refs and $els in for context.
1 parent 1e575a7 commit 48a6c03

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/directives/public/for.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ module.exports = {
202202
var parentScope = this._scope || this.vm
203203
var scope = Object.create(parentScope)
204204
// ref holder for the scope
205-
scope.$refs = {}
206-
scope.$els = {}
205+
scope.$refs = Object.create(parentScope.$refs)
206+
scope.$els = Object.create(parentScope.$els)
207207
// make sure point $parent to parent scope
208208
scope.$parent = parentScope
209209
// for two-way binding on alias

test/unit/specs/directives/public/for/for_spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,41 @@ if (_.inBrowser) {
849849
})
850850
})
851851

852+
it('access parent\'s $refs', function () {
853+
var vm = new Vue({
854+
el: document.createElement('div'),
855+
template: '<c1 v-ref:c1><div v-for="n in 2">{{$refs.c1.d}}</div></c1>',
856+
components: {
857+
c1: {
858+
template: '<div><slot></slot></div>',
859+
data: function () {
860+
return {
861+
d: 1
862+
}
863+
}
864+
}
865+
}
866+
})
867+
expect(vm.$refs.c1 instanceof Vue).toBe(true)
868+
expect(vm.$refs.c1.$el.innerHTML).toContain('<div>1</div><div>1</div>')
869+
})
870+
871+
it('access parent scope\'s $els', function (done) {
872+
var vm = new Vue({
873+
el: document.createElement('div'),
874+
template: '<div data-d=1 v-el:a><div v-for="n in 2">{{ready ? $els.a.dataset.d : 0}}</div></div>',
875+
data: {
876+
ready: false
877+
}
878+
})
879+
expect(vm.$els.a instanceof Element).toBe(true)
880+
expect(vm.$els.a.innerHTML).toContain('<div>0</div><div>0</div>')
881+
vm.ready = true
882+
vm.$nextTick(function () {
883+
expect(vm.$els.a.innerHTML).toContain('<div>1</div><div>1</div>')
884+
done()
885+
})
886+
})
852887
})
853888
}
854889

0 commit comments

Comments
 (0)
0