WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 4d3a046

Browse files
committed
Un-depricate x-init, add returned callback behavior, and depricate x-created & x-mounted
1 parent 28ad366 commit 4d3a046

File tree

6 files changed

+49
-29
lines changed

6 files changed

+49
-29
lines changed

README.md

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Think of it like [Tailwind](https://tailwindcss.com/) for JavaScript.
1414

1515
**From CDN:** Add the following script to the end of your `<head>` section.
1616
```html
17-
<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v1.6.2/dist/alpine.js" defer></script>
17+
<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v1.7.0/dist/alpine.js" defer></script>
1818
```
1919

2020
That's it. It will initialize itself.
@@ -84,14 +84,12 @@ You can even use it for non-trivial things:
8484

8585
## Learn
8686

87-
There are 14 directives available to you:
87+
There are 12 directives available to you:
8888

8989
| Directive
9090
| --- |
9191
| [`x-data`](#x-data) |
9292
| [`x-init`](#x-init) |
93-
| [`x-created`](#x-created) |
94-
| [`x-mounted`](#x-mounted) |
9593
| [`x-show`](#x-show) |
9694
| [`x-bind`](#x-bind) |
9795
| [`x-on`](#x-on) |
@@ -158,32 +156,16 @@ You can also mix-in multiple data objects using object destructuring:
158156

159157
---
160158

161-
> Warning: `x-init` is depricated, in favor of using `x-created` or `x-mounted`. It will be removed in 2.0
162-
163159
### `x-init`
164160
**Example:** `<div x-data"{ foo: 'bar' }" x-init="foo = 'baz"></div>`
165161

166162
**Structure:** `<div x-data="..." x-init="[expression]"></div>`
167163

168164
`x-init` runs an expression when a component is initialized.
169165

170-
---
171-
172-
### `x-created`
173-
**Example:** `<div x-data="{ foo: 'bar' }" x-created="foo = 'baz"></div>`
174-
175-
**Structure:** `<div x-data="..." x-created="[expression]"></div>`
176-
177-
`x-created` runs an expression when a component is initialized, but BEFORE the component initializes the DOM.
178-
179-
---
180-
181-
### `x-mounted`
182-
**Example:** `<div x-data="{ foo: 'bar' }" x-mounted="foo = 'baz"></div>`
183-
184-
**Structure:** `<div x-data="..." x-mounted="[expression]"></div>`
166+
If you wish to run code AFTER Alpine has made it's initial updates to the DOM (something like a `mounted()` hook in VueJS), you can return a callback from `x-init`, and it will be run after:
185167

186-
`x-mounted` runs an expression when a component is initialized, and AFTER the component initializes the DOM.
168+
`x-init="return () => { // we have access to the post-dom-initialization state here // }"`
187169

188170
---
189171

dist/alpine.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/alpine.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"main": "dist/alpine.js",
33
"name": "alpinejs",
4-
"version": "1.6.2",
4+
"version": "1.7.0",
55
"repository": {
66
"type": "git",
77
"url": "git://github.com/alpinejs/alpine.git"

src/component.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ export default class Component {
3535
this.tickStack = []
3636
this.collectingTickCallbacks = false
3737

38+
var initReturnedCallback
3839
if (initExpression) {
39-
console.warn('AlpineJS Warning: "x-init" is depricated and will be removed in the next major version. Use "x-created" instead.')
40+
// We want to allow data manipulation, but not trigger DOM updates just yet.
41+
// We haven't even initialized the elements with their Alpine bindings. I mean c'mon.
4042
this.pauseReactivity = true
41-
saferEvalNoReturn(this.$el.getAttribute('x-init'), this.$data)
43+
initReturnedCallback = saferEval(this.$el.getAttribute('x-init'), this.$data)
4244
this.pauseReactivity = false
4345
}
4446

4547
if (createdExpression) {
46-
// We want to allow data manipulation, but not trigger DOM updates just yet.
47-
// We haven't even initialized the elements with their Alpine bindings. I mean c'mon.
48+
console.warn('AlpineJS Warning: "x-created" is depricated and will be removed in the next major version. Use "x-init" instead.')
4849
this.pauseReactivity = true
4950
saferEvalNoReturn(this.$el.getAttribute('x-created'), this.$data)
5051
this.pauseReactivity = false
@@ -57,7 +58,14 @@ export default class Component {
5758
// Alpine's just so darn flexible amirite?
5859
this.listenForNewElementsToInitialize()
5960

61+
if (typeof initReturnedCallback === 'function') {
62+
// Run the callback returned form the "x-init" hook to allow the user to do stuff after
63+
// Alpine's got it's grubby little paws all over everything.
64+
initReturnedCallback.call(this.$data)
65+
}
66+
6067
if (mountedExpression) {
68+
console.warn('AlpineJS Warning: "x-mounted" is depricated and will be removed in the next major version. Use "x-init" (with a callback return) for the same behavior.')
6169
// Run an "x-mounted" hook to allow the user to do stuff after
6270
// Alpine's got it's grubby little paws all over everything.
6371
saferEvalNoReturn(mountedExpression, this.$data)

test/lifecycle.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,36 @@ test('x-init', async () => {
2222
expect(spanValue).toEqual('baz')
2323
})
2424

25+
test('x-init from data function with callback return for "x-mounted" functionality', async () => {
26+
var valueA
27+
var valueB
28+
window.setValueA = (el) => { valueA = el.innerHTML }
29+
window.setValueB = (el) => { valueB = el.innerText }
30+
window.data = function() {
31+
return {
32+
foo: 'bar',
33+
init() {
34+
window.setValueA(this.$refs.foo)
35+
36+
return () => {
37+
window.setValueB(this.$refs.foo)
38+
}
39+
}
40+
}
41+
}
42+
43+
document.body.innerHTML = `
44+
<div x-data="window.data()" x-init="init()">
45+
<span x-text="foo" x-ref="foo">baz</span>
46+
</div>
47+
`
48+
49+
Alpine.start()
50+
51+
expect(valueA).toEqual('baz')
52+
expect(valueB).toEqual('bar')
53+
})
54+
2555
test('x-created', async () => {
2656
var spanValue
2757
window.setSpanValue = (el) => {

0 commit comments

Comments
 (0)