init.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var Emitter = require('../emitter')
  2. var mergeOptions = require('../util').mergeOptions
  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._data = options.data || {}
  17. this._isDestroyed = false
  18. this._rawContent = null
  19. this._emitter = new Emitter(this)
  20. this._watchers = {}
  21. this._activeWatcher = null
  22. this._directives = []
  23. this._isBlock = false
  24. this._blockStart = null
  25. this._blockEnd = null
  26. // setup parent relationship
  27. this.$parent = options.parent
  28. this._children = []
  29. if (this.$parent) {
  30. this.$parent._children.push(this)
  31. }
  32. // merge options.
  33. this.$options = mergeOptions(
  34. this.constructor.options,
  35. options,
  36. this
  37. )
  38. // the `created` hook is called after basic properties
  39. // have been set up & before data observation happens.
  40. this._callHook('created')
  41. // create scope.
  42. // @creates this.$scope
  43. this._initScope()
  44. // setup initial data.
  45. this._initData(this._data, true)
  46. // setup property proxying
  47. this._initProxy()
  48. // setup computed properties
  49. this._initComputed()
  50. // setup instance methods
  51. this._initMethods()
  52. // setup binding tree.
  53. // @creates this._rootBinding
  54. this._initBindings()
  55. // setup event system and option events
  56. this._initEvents()
  57. // if `el` option is passed, start compilation.
  58. if (options.el) {
  59. this.$mount(options.el)
  60. }
  61. }