@alpinejs/docs 3.8.0-revision.2 → 3.8.0-revision.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -157,9 +157,9 @@ And like most expressions in Alpine, you can always use the result of a JavaScri
|
|
|
157
157
|
<a name="bind-directives"></a>
|
|
158
158
|
## Binding Alpine Directives Directly
|
|
159
159
|
|
|
160
|
-
`x-bind` allows you to bind an object of different
|
|
160
|
+
`x-bind` allows you to bind an object of different directives and attributes to an element.
|
|
161
161
|
|
|
162
|
-
The object keys
|
|
162
|
+
The object keys can be anything you would normally write as an attribute name in Alpine. This includes Alpine directives and modifiers, but also plain HTML attributes. The object values are either plain strings, or in the case of dynamic Alpine directoves, callbacks to be evaluated by Alpine.
|
|
163
163
|
|
|
164
164
|
```alpine
|
|
165
165
|
<div x-data="dropdown()">
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
---
|
|
2
|
+
order: 3
|
|
3
|
+
title: bind()
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Alpine.bind
|
|
7
|
+
|
|
8
|
+
`Alpine.bind(...)` provides a way to re-use [`x-bind`](/directives/bind#bind-directives) objects within your application.
|
|
9
|
+
|
|
10
|
+
Here's a simple example. Rather than binding attributes manually with Alpine:
|
|
11
|
+
|
|
12
|
+
```alpine
|
|
13
|
+
<button type="button" @click="doSomething()" :disabled="shouldDisable"></button>
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
You can bundle these attributes up into a reusable object and use `x-bind` to bind to that:
|
|
17
|
+
|
|
18
|
+
```alpine
|
|
19
|
+
<button x-bind="SomeButton"></button>
|
|
20
|
+
|
|
21
|
+
<script>
|
|
22
|
+
document.addEventListener('alpine:init', () => {
|
|
23
|
+
Alpine.bind('SomeButton', () => ({
|
|
24
|
+
type: 'button',
|
|
25
|
+
|
|
26
|
+
'@click'() {
|
|
27
|
+
this.doSomething()
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
':disabled'() {
|
|
31
|
+
return this.shouldDisable
|
|
32
|
+
},
|
|
33
|
+
}))
|
|
34
|
+
})
|
|
35
|
+
</script>
|
|
36
|
+
```
|