global.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. exports.transition = require('../transition')
  10. /**
  11. * Each instance constructor, including Vue, has a unique
  12. * cid. This enables us to create wrapped "child
  13. * constructors" for prototypal inheritance and cache them.
  14. */
  15. exports.cid = 0
  16. var cid = 1
  17. /**
  18. * Class inehritance
  19. *
  20. * @param {Object} extendOptions
  21. */
  22. exports.extend = function (extendOptions) {
  23. var Super = this
  24. var Sub = function (instanceOptions) {
  25. _.Vue.call(this, instanceOptions)
  26. }
  27. Sub.prototype = Object.create(Super.prototype)
  28. Sub.prototype.constructor = Sub
  29. Sub.cid = cid++
  30. Sub.options = mergeOptions(Super.options, extendOptions)
  31. Sub.super = Super
  32. // allow further extension
  33. Sub.extend = Super.extend
  34. // create asset registers, so extended classes
  35. // can have their private assets too.
  36. createAssetRegisters(Sub)
  37. return Sub
  38. }
  39. /**
  40. * Plugin system
  41. *
  42. * @param {String|Object} plugin
  43. */
  44. exports.use = function (plugin) {
  45. if (typeof plugin === 'string') {
  46. try {
  47. plugin = require(plugin)
  48. } catch (e) {
  49. _.warn('Cannot load plugin: ' + plugin)
  50. }
  51. }
  52. // additional parameters
  53. var args = _.toArray(arguments, 1)
  54. args.unshift(this)
  55. if (typeof plugin.install === 'function') {
  56. plugin.install.apply(plugin, args)
  57. } else {
  58. plugin.apply(null, args)
  59. }
  60. return this
  61. }
  62. /**
  63. * Define asset registration methods on a constructor.
  64. *
  65. * @param {Function} Constructor
  66. */
  67. var assetTypes = [
  68. 'directive',
  69. 'filter',
  70. 'partial',
  71. 'transition'
  72. ]
  73. function createAssetRegisters (Constructor) {
  74. /* Asset registration methods share the same signature:
  75. *
  76. * @param {String} id
  77. * @param {*} definition
  78. */
  79. assetTypes.forEach(function (type) {
  80. Constructor[type] = function (id, definition) {
  81. if (!definition) {
  82. return this.options[type + 's'][id]
  83. } else {
  84. this.options[type + 's'][id] = definition
  85. }
  86. }
  87. })
  88. /**
  89. * Component registration needs to automatically invoke
  90. * Vue.extend on object values.
  91. *
  92. * @param {String} id
  93. * @param {Object|Function} definition
  94. */
  95. Constructor.component = function (id, definition) {
  96. if (!definition) {
  97. return this.options.components[id]
  98. } else {
  99. if (_.isPlainObject(definition)) {
  100. definition = _.Vue.extend(definition)
  101. }
  102. this.options.components[id] = definition
  103. }
  104. }
  105. }
  106. createAssetRegisters(exports)