init.js 2.8 KB

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