component.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. var _ = require('../util')
  2. var config = require('../config')
  3. var templateParser = require('../parsers/template')
  4. module.exports = {
  5. isLiteral: true,
  6. /**
  7. * Setup. Two possible usages:
  8. *
  9. * - static:
  10. * v-component="comp"
  11. *
  12. * - dynamic:
  13. * v-component="{{currentView}}"
  14. */
  15. bind: function () {
  16. if (!this.el.__vue__) {
  17. // create a ref anchor
  18. this.anchor = _.createAnchor('v-component')
  19. _.replace(this.el, this.anchor)
  20. // check keep-alive options.
  21. // If yes, instead of destroying the active vm when
  22. // hiding (v-if) or switching (dynamic literal) it,
  23. // we simply remove it from the DOM and save it in a
  24. // cache object, with its constructor id as the key.
  25. this.keepAlive = this._checkParam('keep-alive') != null
  26. // wait for event before insertion
  27. this.readyEvent = this._checkParam('wait-for')
  28. // check ref
  29. this.refID = this._checkParam(config.prefix + 'ref')
  30. if (this.keepAlive) {
  31. this.cache = {}
  32. }
  33. // check inline-template
  34. if (this._checkParam('inline-template') !== null) {
  35. // extract inline template as a DocumentFragment
  36. this.template = _.extractContent(this.el, true)
  37. }
  38. // component resolution related state
  39. this._pendingCb =
  40. this.ctorId =
  41. this.Ctor = null
  42. // if static, build right now.
  43. if (!this._isDynamicLiteral) {
  44. this.resolveCtor(this.expression, _.bind(this.initStatic, this))
  45. } else {
  46. // check dynamic component params
  47. this.transMode = this._checkParam('transition-mode')
  48. }
  49. } else {
  50. process.env.NODE_ENV !== 'production' && _.warn(
  51. 'cannot mount component "' + this.expression + '" ' +
  52. 'on already mounted element: ' + this.el
  53. )
  54. }
  55. },
  56. /**
  57. * Initialize a static component.
  58. */
  59. initStatic: function () {
  60. var child = this.build()
  61. var anchor = this.anchor
  62. this.setCurrent(child)
  63. if (!this.readyEvent) {
  64. child.$before(anchor)
  65. } else {
  66. child.$once(this.readyEvent, function () {
  67. child.$before(anchor)
  68. })
  69. }
  70. },
  71. /**
  72. * Public update, called by the watcher in the dynamic
  73. * literal scenario, e.g. v-component="{{view}}"
  74. */
  75. update: function (value) {
  76. this.setComponent(value)
  77. },
  78. /**
  79. * Switch dynamic components. May resolve the component
  80. * asynchronously, and perform transition based on
  81. * specified transition mode. Accepts a few additional
  82. * arguments specifically for vue-router.
  83. *
  84. * @param {String} value
  85. * @param {Object} data
  86. * @param {Function} afterBuild
  87. * @param {Function} afterTransition
  88. */
  89. setComponent: function (value, data, afterBuild, afterTransition) {
  90. this.invalidatePending()
  91. if (!value) {
  92. // just remove current
  93. this.unbuild(true)
  94. this.remove(this.childVM, afterTransition)
  95. this.unsetCurrent()
  96. } else {
  97. this.resolveCtor(value, _.bind(function () {
  98. this.unbuild(true)
  99. var newComponent = this.build(data)
  100. /* istanbul ignore if */
  101. if (afterBuild) afterBuild(newComponent)
  102. var self = this
  103. if (this.readyEvent) {
  104. newComponent.$once(this.readyEvent, function () {
  105. self.transition(newComponent, afterTransition)
  106. })
  107. } else {
  108. this.transition(newComponent, afterTransition)
  109. }
  110. }, this))
  111. }
  112. },
  113. /**
  114. * Resolve the component constructor to use when creating
  115. * the child vm.
  116. */
  117. resolveCtor: function (id, cb) {
  118. var self = this
  119. this._pendingCb = _.cancellable(function (ctor) {
  120. self.ctorId = id
  121. self.Ctor = ctor
  122. cb()
  123. })
  124. this.vm._resolveComponent(id, this._pendingCb)
  125. },
  126. /**
  127. * When the component changes or unbinds before an async
  128. * constructor is resolved, we need to invalidate its
  129. * pending callback.
  130. */
  131. invalidatePending: function () {
  132. if (this._pendingCb) {
  133. this._pendingCb.cancel()
  134. this._pendingCb = null
  135. }
  136. },
  137. /**
  138. * Instantiate/insert a new child vm.
  139. * If keep alive and has cached instance, insert that
  140. * instance; otherwise build a new one and cache it.
  141. *
  142. * @param {Object} [data]
  143. * @return {Vue} - the created instance
  144. */
  145. build: function (data) {
  146. if (this.keepAlive) {
  147. var cached = this.cache[this.ctorId]
  148. if (cached) {
  149. return cached
  150. }
  151. }
  152. if (this.Ctor) {
  153. var parent = this._host || this.vm
  154. var el = templateParser.clone(this.el)
  155. var child = parent.$addChild({
  156. el: el,
  157. data: data,
  158. template: this.template,
  159. // if no inline-template, then the compiled
  160. // linker can be cached for better performance.
  161. _linkerCachable: !this.template,
  162. _asComponent: true,
  163. _isRouterView: this._isRouterView,
  164. _context: this.vm
  165. }, this.Ctor)
  166. if (this.keepAlive) {
  167. this.cache[this.ctorId] = child
  168. }
  169. return child
  170. }
  171. },
  172. /**
  173. * Teardown the current child, but defers cleanup so
  174. * that we can separate the destroy and removal steps.
  175. *
  176. * @param {Boolean} defer
  177. */
  178. unbuild: function (defer) {
  179. var child = this.childVM
  180. if (!child || this.keepAlive) {
  181. return
  182. }
  183. // the sole purpose of `deferCleanup` is so that we can
  184. // "deactivate" the vm right now and perform DOM removal
  185. // later.
  186. child.$destroy(false, defer)
  187. },
  188. /**
  189. * Remove current destroyed child and manually do
  190. * the cleanup after removal.
  191. *
  192. * @param {Function} cb
  193. */
  194. remove: function (child, cb) {
  195. var keepAlive = this.keepAlive
  196. if (child) {
  197. child.$remove(function () {
  198. if (!keepAlive) child._cleanup()
  199. if (cb) cb()
  200. })
  201. } else if (cb) {
  202. cb()
  203. }
  204. },
  205. /**
  206. * Actually swap the components, depending on the
  207. * transition mode. Defaults to simultaneous.
  208. *
  209. * @param {Vue} target
  210. * @param {Function} [cb]
  211. */
  212. transition: function (target, cb) {
  213. var self = this
  214. var current = this.childVM
  215. this.unsetCurrent()
  216. this.setCurrent(target)
  217. switch (self.transMode) {
  218. case 'in-out':
  219. target.$before(self.anchor, function () {
  220. self.remove(current, cb)
  221. })
  222. break
  223. case 'out-in':
  224. self.remove(current, function () {
  225. if (!target._isDestroyed) {
  226. target.$before(self.anchor, cb)
  227. }
  228. })
  229. break
  230. default:
  231. self.remove(current)
  232. target.$before(self.anchor, cb)
  233. }
  234. },
  235. /**
  236. * Set childVM and parent ref
  237. */
  238. setCurrent: function (child) {
  239. this.childVM = child
  240. var refID = child._refID || this.refID
  241. if (refID) {
  242. this.vm.$[refID] = child
  243. }
  244. },
  245. /**
  246. * Unset childVM and parent ref
  247. */
  248. unsetCurrent: function () {
  249. var child = this.childVM
  250. this.childVM = null
  251. var refID = (child && child._refID) || this.refID
  252. if (refID) {
  253. this.vm.$[refID] = null
  254. }
  255. },
  256. /**
  257. * Unbind.
  258. */
  259. unbind: function () {
  260. this.invalidatePending()
  261. // Do not defer cleanup when unbinding
  262. this.unbuild()
  263. this.unsetCurrent()
  264. // destroy all keep-alive cached instances
  265. if (this.cache) {
  266. for (var key in this.cache) {
  267. this.cache[key].$destroy()
  268. }
  269. this.cache = null
  270. }
  271. }
  272. }