Skip to content

Commit ea34d56

Browse files
committed
hook inheritance
1 parent fede1eb commit ea34d56

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

src/main.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ function extend (options) {
7474
options = inheritOptions(options, ParentVM.options, true)
7575
utils.processOptions(options)
7676

77-
var ExtendedVM = function (opts) {
78-
opts = inheritOptions(opts, options, true)
79-
ParentVM.call(this, opts)
77+
var ExtendedVM = function (opts, asParent) {
78+
if (!asParent) {
79+
opts = inheritOptions(opts, options, true)
80+
}
81+
ParentVM.call(this, opts, true)
8082
}
8183

8284
// inherit prototype props
@@ -118,15 +120,34 @@ function inheritOptions (child, parent, topLevel) {
118120
if (!parent) return child
119121
for (var key in parent) {
120122
if (key === 'el' || key === 'proto') continue
121-
if (!child[key]) { // child has priority
122-
child[key] = parent[key]
123-
} else if (topLevel && utils.typeOf(child[key]) === 'Object') {
124-
inheritOptions(child[key], parent[key], false)
123+
var val = child[key],
124+
parentVal = parent[key],
125+
type = utils.typeOf(val)
126+
if (topLevel && type === 'Function' && parentVal) {
127+
// merge hook functions
128+
child[key] = mergeHook(val, parentVal)
129+
} else if (topLevel && type === 'Object') {
130+
// merge toplevel object options
131+
inheritOptions(val, parentVal)
132+
} else if (val === undefined) {
133+
// inherit if child doesn't override
134+
child[key] = parentVal
125135
}
126136
}
127137
return child
128138
}
129139

140+
/**
141+
* Merge hook functions
142+
* so parent hooks also get called
143+
*/
144+
function mergeHook (fn, parentFn) {
145+
return function (opts) {
146+
parentFn.call(this, opts)
147+
fn.call(this, opts)
148+
}
149+
}
150+
130151
/**
131152
* Update prefix for some special directives
132153
* that are used in compilation.

test/functional/fixtures/encapsulation.html renamed to test/functional/fixtures/extend.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,26 @@
66
<script src="../../../dist/vue.js"></script>
77
</head>
88
<body>
9+
<div id="log"></div>
910
<div id="test">
1011
<div class="dir" v-hola="dirMsg"></div>
1112
<div class="filter">{{filterMsg | nodigits}}</div>
1213
<div class="partial" v-partial="partial-test"></div>
1314
<div class="vm" v-component="vm-test">{{vmMsg}}</div>
1415
<div class="vm-w-model" v-component="vm-w-model:vmData">{{msg + model.msg}}</div>
1516
</div>
17+
<div id="child">
18+
<div class="cvm" v-component="vm-test">{{vmMsg}}</div>
19+
</div>
1620
<script>
21+
var log = document.getElementById('log')
1722
var T = Vue.extend({
23+
created: function () {
24+
log.textContent += ' T created'
25+
},
26+
ready: function () {
27+
log.textContent += ' T ready'
28+
},
1829
components: {
1930
'vm-test': {
2031
scope: {
@@ -41,6 +52,14 @@
4152
}
4253
}
4354
})
55+
var C = T.extend({
56+
created: function () {
57+
log.textContent += ' C created'
58+
},
59+
ready: function () {
60+
log.textContent += ' C ready'
61+
}
62+
})
4463
new T({
4564
el: '#test',
4665
scope: {
@@ -52,6 +71,9 @@
5271
}
5372
}
5473
})
74+
new C({
75+
el: '#child'
76+
})
5577
</script>
5678
</body>
5779
</html>

test/functional/specs/encapsulation.js renamed to test/functional/specs/extend.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
casper.test.begin('Component Encapsulation', 5, function (test) {
1+
casper.test.begin('Encapsulation & Inheritance', 7, function (test) {
22

33
casper
4-
.start('./fixtures/encapsulation.html', function () {
4+
.start('./fixtures/extend.html', function () {
5+
56
test.assertSelectorHasText('.dir', 'directive works')
67
test.assertSelectorHasText('.filter', 'filter works')
78
test.assertSelectorHasText('.partial', 'partial works')
89
test.assertSelectorHasText('.vm', 'component works')
910
test.assertSelectorHasText('.vm-w-model', 'component with model works')
11+
12+
test.assertSelectorHasText('#log', 'T created T ready T created C created T ready C ready', 'hook inheritance works')
13+
test.assertSelectorHasText('.cvm', 'component works', 'Child should have access to Parent options')
1014
})
1115
.run(function () {
1216
test.done()

0 commit comments

Comments
 (0)