Skip to content

Commit 7b24395

Browse files
committed
make vm.$get recursive
1 parent d5c182f commit 7b24395

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/viewmodel.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ var VMProto = ViewModel.prototype
2828
* Convenience function to get a value from
2929
* a keypath
3030
*/
31-
def(VMProto, '$get', function (key, value) {
32-
return utils.get(this, key, value)
31+
def(VMProto, '$get', function (key) {
32+
var val = utils.get(this, key)
33+
return val === undefined && this.$parent
34+
? this.$parent.$get(key)
35+
: val
3336
})
3437

3538
/**

test/unit/specs/viewmodel.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,28 @@ describe('ViewModel', function () {
99
}
1010
},
1111
arr = [1, 2, 3],
12+
parentVM = new Vue({
13+
data: { fromParent: 'hello' }
14+
}),
1215
vm = new Vue({
1316
el: '#vm-test',
17+
parent: parentVM,
1418
data: {
1519
a: data,
1620
b: arr
1721
}
1822
})
1923

2024
describe('.$get()', function () {
21-
it('should set correct value', function () {
25+
it('should get correct value', function () {
2226
var v = vm.$get('a.b.c')
2327
assert.strictEqual(v, 12345)
2428
})
29+
30+
it('should recursively get value from parents', function () {
31+
var v = vm.$get('fromParent')
32+
assert.strictEqual(v, 'hello')
33+
})
2534
})
2635

2736
describe('.$set()', function () {

0 commit comments

Comments
 (0)