misc.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {
  2. resolveAsset,
  3. assertAsset,
  4. isPlainObject,
  5. warn
  6. } from '../../util/index'
  7. export default function (Vue) {
  8. /**
  9. * Apply a list of filter (descriptors) to a value.
  10. * Using plain for loops here because this will be called in
  11. * the getter of any watcher with filters so it is very
  12. * performance sensitive.
  13. *
  14. * @param {*} value
  15. * @param {*} [oldValue]
  16. * @param {Array} filters
  17. * @param {Boolean} write
  18. * @return {*}
  19. */
  20. Vue.prototype._applyFilters = function (value, oldValue, filters, write) {
  21. var filter, fn, args, arg, offset, i, l, j, k
  22. for (i = 0, l = filters.length; i < l; i++) {
  23. filter = filters[i]
  24. fn = resolveAsset(this.$options, 'filters', filter.name)
  25. if (process.env.NODE_ENV !== 'production') {
  26. assertAsset(fn, 'filter', filter.name)
  27. }
  28. if (!fn) continue
  29. fn = write ? fn.write : (fn.read || fn)
  30. if (typeof fn !== 'function') continue
  31. args = write ? [value, oldValue] : [value]
  32. offset = write ? 2 : 1
  33. if (filter.args) {
  34. for (j = 0, k = filter.args.length; j < k; j++) {
  35. arg = filter.args[j]
  36. args[j + offset] = arg.dynamic
  37. ? this.$get(arg.value)
  38. : arg.value
  39. }
  40. }
  41. value = fn.apply(this, args)
  42. }
  43. return value
  44. }
  45. /**
  46. * Resolve a component, depending on whether the component
  47. * is defined normally or using an async factory function.
  48. * Resolves synchronously if already resolved, otherwise
  49. * resolves asynchronously and caches the resolved
  50. * constructor on the factory.
  51. *
  52. * @param {String} id
  53. * @param {Function} cb
  54. */
  55. Vue.prototype._resolveComponent = function (id, cb) {
  56. var factory = resolveAsset(this.$options, 'components', id)
  57. if (process.env.NODE_ENV !== 'production') {
  58. assertAsset(factory, 'component', id)
  59. }
  60. if (!factory) {
  61. return
  62. }
  63. // async component factory
  64. if (!factory.options) {
  65. if (factory.resolved) {
  66. // cached
  67. cb(factory.resolved)
  68. } else if (factory.requested) {
  69. // pool callbacks
  70. factory.pendingCallbacks.push(cb)
  71. } else {
  72. factory.requested = true
  73. var cbs = factory.pendingCallbacks = [cb]
  74. factory.call(this, function resolve (res) {
  75. if (isPlainObject(res)) {
  76. res = Vue.extend(res)
  77. }
  78. // cache resolved
  79. factory.resolved = res
  80. // invoke callbacks
  81. for (var i = 0, l = cbs.length; i < l; i++) {
  82. cbs[i](res)
  83. }
  84. }, function reject (reason) {
  85. process.env.NODE_ENV !== 'production' && warn(
  86. 'Failed to resolve async component: ' + id + '. ' +
  87. (reason ? '\nReason: ' + reason : '')
  88. )
  89. })
  90. }
  91. } else {
  92. // normal component
  93. cb(factory)
  94. }
  95. }
  96. }