init.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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._eventCancelled = false // for event cancellation
  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. // push self into parent / transclusion host
  52. if (this.$parent) {
  53. this.$parent.$children.push(this)
  54. }
  55. // props used in v-repeat diffing
  56. this._reused = false
  57. this._staggerOp = null
  58. // merge options.
  59. options = this.$options = mergeOptions(
  60. this.constructor.options,
  61. options,
  62. this
  63. )
  64. // initialize data as empty object.
  65. // it will be filled up in _initScope().
  66. this._data = {}
  67. // initialize data observation and scope inheritance.
  68. this._initScope()
  69. // setup event system and option events.
  70. this._initEvents()
  71. // call created hook
  72. this._callHook('created')
  73. // if `el` option is passed, start compilation.
  74. if (options.el) {
  75. this.$mount(options.el)
  76. }
  77. }