Skip to content

Commit 96e08fb

Browse files
committed
component: refactor for hot reloading
1 parent 8ceaf9e commit 96e08fb

File tree

1 file changed

+37
-42
lines changed

1 file changed

+37
-42
lines changed

src/directives/internal/component.js

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,14 @@ module.exports = {
4545
// transition related state
4646
this.pendingRemovals = 0
4747
this.pendingRemovalCb = null
48+
// check dynamic component params
49+
// create a ref anchor
50+
this.anchor = _.createAnchor('v-component')
51+
_.replace(this.el, this.anchor)
52+
this.transMode = this.param('transition-mode')
4853
// if static, build right now.
4954
if (this.literal) {
50-
this.resolveComponent(this.expression, _.bind(this.initStatic, this))
51-
} else {
52-
// check dynamic component params
53-
// create a ref anchor
54-
this.anchor = _.createAnchor('v-component')
55-
_.replace(this.el, this.anchor)
56-
this.transMode = this.param('transition-mode')
55+
this.setComponent(this.expression)
5756
}
5857
} else {
5958
process.env.NODE_ENV !== 'production' && _.warn(
@@ -63,25 +62,6 @@ module.exports = {
6362
}
6463
},
6564

66-
/**
67-
* Initialize a static component.
68-
*/
69-
70-
initStatic: function () {
71-
var anchor = this.el
72-
var activateHook = this.Component.options.activate
73-
var child = this.childVM = this.build()
74-
if (activateHook) {
75-
activateHook.call(child, insert)
76-
} else {
77-
insert()
78-
}
79-
function insert () {
80-
child.$before(anchor)
81-
_.remove(anchor)
82-
}
83-
},
84-
8565
/**
8666
* Public update, called by the watcher in the dynamic
8767
* literal scenario, e.g. <component :is="view">
@@ -114,22 +94,10 @@ module.exports = {
11494
this.remove(this.childVM, cb)
11595
this.childVM = null
11696
} else {
117-
this.resolveComponent(value, _.bind(function () {
118-
this.unbuild(true)
119-
var self = this
120-
var activateHook = this.Component.options.activate
121-
var cached = this.getCached()
122-
var newComponent = this.build()
123-
if (activateHook && !cached) {
124-
this.waitingFor = newComponent
125-
activateHook.call(newComponent, function () {
126-
self.waitingFor = null
127-
self.transition(newComponent, cb)
128-
})
129-
} else {
130-
this.transition(newComponent, cb)
131-
}
132-
}, this))
97+
var self = this
98+
this.resolveComponent(value, function () {
99+
self.mountComponent(cb)
100+
})
133101
}
134102
},
135103

@@ -147,6 +115,33 @@ module.exports = {
147115
this.vm._resolveComponent(id, this.pendingComponentCb)
148116
},
149117

118+
/**
119+
* Create a new instance using the current constructor and
120+
* replace the existing instance. This method doesn't care
121+
* whether the new component and the old one are actually
122+
* the same.
123+
*
124+
* @param {Function} [cb]
125+
*/
126+
127+
mountComponent: function (cb) {
128+
// actual mount
129+
this.unbuild(true)
130+
var self = this
131+
var activateHook = this.Component.options.activate
132+
var cached = this.getCached()
133+
var newComponent = this.build()
134+
if (activateHook && !cached) {
135+
this.waitingFor = newComponent
136+
activateHook.call(newComponent, function () {
137+
self.waitingFor = null
138+
self.transition(newComponent, cb)
139+
})
140+
} else {
141+
this.transition(newComponent, cb)
142+
}
143+
},
144+
150145
/**
151146
* When the component changes or unbinds before an async
152147
* constructor is resolved, we need to invalidate its

0 commit comments

Comments
 (0)