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