init.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. var mergeOptions = require('../util').mergeOptions
  2. var uid = 0
  3. /**
  4. * The main init sequence. This is called for every
  5. * instance, including ones that are created from extended
  6. * constructors.
  7. *
  8. * @param {Object} options - this options object should be
  9. * the result of merging class
  10. * options and the options passed
  11. * in to the constructor.
  12. */
  13. exports._init = function (options) {
  14. options = options || {}
  15. this.$el = null
  16. this.$parent = options.parent
  17. this.$root = this.$parent
  18. ? this.$parent.$root
  19. : this
  20. this.$children = []
  21. this.$refs = {} // child vm references
  22. this.$els = {} // element references
  23. this._watchers = [] // all watchers as an array
  24. this._directives = [] // all directives
  25. // a uid
  26. this._uid = uid++
  27. // a flag to avoid this being observed
  28. this._isVue = true
  29. // events bookkeeping
  30. this._events = {} // registered callbacks
  31. this._eventsCount = {} // for $broadcast optimization
  32. this._shouldPropagate = false // for event propagation
  33. // fragment instance properties
  34. this._isFragment = false
  35. this._fragment = // @type {DocumentFragment}
  36. this._fragmentStart = // @type {Text|Comment}
  37. this._fragmentEnd = null // @type {Text|Comment}
  38. // lifecycle state
  39. this._isCompiled =
  40. this._isDestroyed =
  41. this._isReady =
  42. this._isAttached =
  43. this._isBeingDestroyed = false
  44. this._unlinkFn = null
  45. // context:
  46. // if this is a transcluded component, context
  47. // will be the common parent vm of this instance
  48. // and its host.
  49. this._context = options._context || this.$parent
  50. // scope:
  51. // if this is inside an inline v-for, the scope
  52. // will be the intermediate scope created for this
  53. // repeat fragment. this is used for linking props
  54. // and container directives.
  55. this._scope = options._scope
  56. // fragment:
  57. // if this instance is compiled inside a Fragment, it
  58. // needs to reigster itself as a child of that fragment
  59. // for attach/detach to work properly.
  60. this._frag = options._frag
  61. if (this._frag) {
  62. this._frag.children.push(this)
  63. }
  64. // push self into parent / transclusion host
  65. if (this.$parent) {
  66. this.$parent.$children.push(this)
  67. }
  68. // merge options.
  69. options = this.$options = mergeOptions(
  70. this.constructor.options,
  71. options,
  72. this
  73. )
  74. // set ref
  75. this._updateRef()
  76. // initialize data as empty object.
  77. // it will be filled up in _initScope().
  78. this._data = {}
  79. // call init hook
  80. this._callHook('init')
  81. // initialize data observation and scope inheritance.
  82. this._initState()
  83. // setup event system and option events.
  84. this._initEvents()
  85. // call created hook
  86. this._callHook('created')
  87. // if `el` option is passed, start compilation.
  88. if (options.el) {
  89. this.$mount(options.el)
  90. }
  91. }