Creating a custom directive #1815
-
Hi, I need to bind to element properties instead of attributes, and the only way I found to achieve this task was creating a custom directive called Let's say my element have a custom property called <body x-data="data"
ps-init>
<my-element x-prop:myprop="obj"></my-element>
<script>
const data = {
obj: {
key1: 'value1',
key2: 'value2'
}
}
</script>
</body> I wrote the code below for my custom directive. Alpine.directive('prop', (el: any, { value, expression }: any, { effect }: any) => {
const evaluate = Alpine.evaluateLater(el, expression);
effect(() => evaluate((result: any) => {
if (!el._x_bindings) el._x_bindings = Alpine.reactive({});
el._x_bindings[value] = result;
Alpine.mutateDom(() => {
el[value] = result;
});
}));
}); The code is working, but when it evaluates the expression, the Is there a way I can access the proxy's target from the Fabiano |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
I think you want to use |
Beta Was this translation helpful? Give feedback.
I think you want to use
closestRoot(el)
instead ofel
. If you get stuck and put it on codepen or elsewhere I can show you