global.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var _ = require('../util')
  2. var mergeOptions = require('../util/merge-option')
  3. /**
  4. * Expose useful internals
  5. */
  6. exports.util = _
  7. exports.nextTick = _.nextTick
  8. exports.config = require('../config')
  9. /**
  10. * Each instance constructor, including Vue, has a unique
  11. * cid. This enables us to create wrapped "child
  12. * constructors" for prototypal inheritance and cache them.
  13. */
  14. exports.cid = 0
  15. var cid = 1
  16. /**
  17. * Class inehritance
  18. *
  19. * @param {Object} extendOptions
  20. */
  21. exports.extend = function (extendOptions) {
  22. extendOptions = extendOptions || {}
  23. var Super = this
  24. var Sub = createClass(extendOptions.name || 'VueComponent')
  25. Sub.prototype = Object.create(Super.prototype)
  26. Sub.prototype.constructor = Sub
  27. Sub.cid = cid++
  28. Sub.options = mergeOptions(
  29. Super.options,
  30. extendOptions
  31. )
  32. Sub['super'] = Super
  33. // allow further extension
  34. Sub.extend = Super.extend
  35. // create asset registers, so extended classes
  36. // can have their private assets too.
  37. createAssetRegisters(Sub)
  38. return Sub
  39. }
  40. /**
  41. * A function that returns a sub-class constructor with the
  42. * given name. This gives us much nicer output when
  43. * logging instances in the console.
  44. *
  45. * @param {String} name
  46. * @return {Function}
  47. */
  48. function createClass (name) {
  49. return new Function(
  50. 'return function ' + _.camelize(name, true) +
  51. ' (options) { this._init(options) }'
  52. )()
  53. }
  54. /**
  55. * Plugin system
  56. *
  57. * @param {Object} plugin
  58. */
  59. exports.use = function (plugin) {
  60. // additional parameters
  61. var args = _.toArray(arguments, 1)
  62. args.unshift(this)
  63. if (typeof plugin.install === 'function') {
  64. plugin.install.apply(plugin, args)
  65. } else {
  66. plugin.apply(null, args)
  67. }
  68. return this
  69. }
  70. /**
  71. * Define asset registration methods on a constructor.
  72. *
  73. * @param {Function} Constructor
  74. */
  75. var assetTypes = [
  76. 'directive',
  77. 'filter',
  78. 'partial',
  79. 'transition'
  80. ]
  81. function createAssetRegisters (Constructor) {
  82. /* Asset registration methods share the same signature:
  83. *
  84. * @param {String} id
  85. * @param {*} definition
  86. */
  87. assetTypes.forEach(function (type) {
  88. Constructor[type] = function (id, definition) {
  89. if (!definition) {
  90. return this.options[type + 's'][id]
  91. } else {
  92. this.options[type + 's'][id] = definition
  93. }
  94. }
  95. })
  96. /**
  97. * Component registration needs to automatically invoke
  98. * Vue.extend on object values.
  99. *
  100. * @param {String} id
  101. * @param {Object|Function} definition
  102. */
  103. Constructor.component = function (id, definition) {
  104. if (!definition) {
  105. return this.options.components[id]
  106. } else {
  107. if (_.isPlainObject(definition)) {
  108. definition.name = id
  109. definition = _.Vue.extend(definition)
  110. }
  111. this.options.components[id] = definition
  112. }
  113. }
  114. }
  115. createAssetRegisters(exports)