$init helper #91
-
The helper would offer an alternative, more declarative way to x-init, there may be some overlapping with cache depending on the usage. Possibly we can support This should be achieved with a control boolean in the helper set to false. When the elper resolves, if the control boolean is false, it evaluate the expression, set the control flag to true and assign the result to foo. If the value is true, it will just return foo. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Note, it should support promises and return a noop proxy until the promise is resolved, then it should update the variable (i was initially thinking about an $await helper but it makes sense to implement it here to avoid duplication). |
Beta Was this translation helpful? Give feedback.
-
The only worry I have with any sort of init type functionality is that Caleb mentioned in (alpinejs/alpine#1015 (comment)) that x-init will behave differently in V3 so it might be good to wait and see how it changes and whether there's a better way to extend it.
What you described sounds more like <div x-data="{ product: 'Loading...' }">
<p x-text="$waitFor(
$cache('product', () => expensiveLongCalculation(), 300),
(result) => result.description
)">
The example is messing data types a bit much, but that's optional use anyway. it could be further abstracted with something like Am I way off here? :) Anyway just brainstorming. |
Beta Was this translation helpful? Give feedback.
The only worry I have with any sort of init type functionality is that Caleb mentioned in (alpinejs/alpine#1015 (comment)) that x-init will behave differently in V3 so it might be good to wait and see how it changes and whether there's a better way to extend it.
What you described sounds more like
x-text="$waitFor('foo', (v) => v.bar)"
or$waitFor()
would be waiting for an initial change (instead of…