init.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.$ = {} // child vm references
  18. this.$$ = {} // element references
  19. this._watchers = [] // all watchers as an array
  20. this._directives = [] // all directives
  21. // a flag to avoid this being observed
  22. this._isVue = true
  23. // events bookkeeping
  24. this._events = {} // registered callbacks
  25. this._eventsCount = {} // for $broadcast optimization
  26. this._eventCancelled = false // for event cancellation
  27. // block instance properties
  28. this._isBlock = false
  29. this._blockStart = // @type {CommentNode}
  30. this._blockEnd = null // @type {CommentNode}
  31. // lifecycle state
  32. this._isCompiled =
  33. this._isDestroyed =
  34. this._isReady =
  35. this._isAttached =
  36. this._isBeingDestroyed = false
  37. this._unlinkFn = null
  38. // children
  39. this._children = []
  40. this._childCtors = {}
  41. // transcluded components that belong to the parent.
  42. // need to keep track of them so that we can call
  43. // attached/detached hooks on them.
  44. this._transCpnts = []
  45. this._host = options._host
  46. // push self into parent / transclusion host
  47. if (this.$parent) {
  48. this.$parent._children.push(this)
  49. }
  50. if (this._host) {
  51. this._host._transCpnts.push(this)
  52. }
  53. // props used in v-repeat diffing
  54. this._new = true
  55. this._reused = false
  56. // merge options.
  57. options = this.$options = mergeOptions(
  58. this.constructor.options,
  59. options,
  60. this
  61. )
  62. // set data after merge.
  63. this._data = options.data || {}
  64. // initialize data observation and scope inheritance.
  65. this._initScope()
  66. // setup event system and option events.
  67. this._initEvents()
  68. // call created hook
  69. this._callHook('created')
  70. // if `el` option is passed, start compilation.
  71. if (options.el) {
  72. this.$mount(options.el)
  73. }
  74. }