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
|
import { attributesOnly, directive, directives, into, mapAttributes, prefix, startingWith } from '../directives'
import { evaluateLater } from '../evaluator'
import { mutateDom } from '../mutation'
import bind from '../utils/bind'
mapAttributes(startingWith(':', into(prefix('bind:'))))
directive('bind', (el, { value, modifiers, expression, original }, { effect }) => {
if (! value) return applyBindingsObject(el, expression, original, effect)
if (value === 'key') return storeKeyForXFor(el, expression)
let evaluate = evaluateLater(el, expression)
effect(() => evaluate(result => {
// If nested object key is undefined, set the default value to empty string.
if (result === undefined && expression.match(/\./)) result = ''
mutateDom(() => bind(el, value, result, modifiers))
}))
})
function applyBindingsObject(el, expression, original, effect) {
let getBindings = evaluateLater(el, expression)
let cleanupRunners = []
effect(() => {
while (cleanupRunners.length) cleanupRunners.pop()()
getBindings(bindings => {
let attributes = Object.entries(bindings).map(([name, value]) => ({ name, value }))
// Handle binding normal HTML attributes (non-Alpine directives).
attributesOnly(attributes).forEach(({ name, value }, index) => {
attributes[index] = {
name: `x-bind:${name}`,
value: `"${value}"`,
}
})
directives(el, attributes, original).map(handle => {
cleanupRunners.push(handle.runCleanups)
handle()
})
})
})
}
function storeKeyForXFor(el, expression) {
el._x_keyExpression = expression
}
|