init.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var mergeOptions = require('../util/merge-option')
  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._watcherList = [] // all watchers as an array
  20. this._watchers = {} // internal watchers as a hash
  21. this._userWatchers = {} // user watchers as a hash
  22. this._directives = [] // all directives
  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. // block instance properties
  30. this._isBlock = false
  31. this._blockStart = // @type {CommentNode}
  32. this._blockEnd = null // @type {CommentNode}
  33. // lifecycle state
  34. this._isCompiled =
  35. this._isDestroyed =
  36. this._isReady =
  37. this._isAttached =
  38. this._isBeingDestroyed = false
  39. // children
  40. this._children = // @type {Array}
  41. this._childCtors = null // @type {Object} - hash to cache
  42. // child constructors
  43. // anonymous instances are created by v-if
  44. // we need to walk along the parent chain to locate the
  45. // first non-anonymous instance as the owner.
  46. this._isAnonymous = options._anonymous
  47. var parent = this.$parent
  48. while (parent && parent._isAnonymous) {
  49. parent = parent.$parent
  50. }
  51. this._owner = parent || this
  52. // merge options.
  53. options = this.$options = mergeOptions(
  54. this.constructor.options,
  55. options,
  56. this
  57. )
  58. // set data after merge.
  59. this._data = options.data || {}
  60. // setup event system and option events.
  61. this._initEvents()
  62. // initialize data observation and scope inheritance.
  63. this._initScope()
  64. // call created hook
  65. this._callHook('created')
  66. // if `el` option is passed, start compilation.
  67. if (options.el) {
  68. this.$mount(options.el)
  69. }
  70. }