-
I'm seeing in some of the directive patterns that DOM manipulation follows this pattern: mutateDom(
() => {
el.after( clone );
skipDuringClone(
() => {
initTree( clone );
}
)();
}
); However, in some personal experimentation, things seem to work if I get rid of the mutateDom(
() => {
el.after( clone );
initTree( clone );
}
); I've tried searching Google, the forums, and the Definitely Typed |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
So some directives, declare themselves as being skippable in clone contexts, because of how cloning itself has already handled certain things the directive does. skipDuringClone causes those directives to not initialize/do the things that are already handled by cloning. I think it's only one or two directives that need this. I think x-data is a main one for this. Since cloning will have copied the exact current state of the element. If you reevaluate x-data you'd reset the data and end up with an extra stack. |
Beta Was this translation helpful? Give feedback.
-
@ekwoka ah, ok. So, unless I have a very specific reason to do that, I can just skip (no pun intended) calling this method. That said, it seems that all of the structural directives do make use of the Case-in-point, I'm working on a directive that clones a template reference and renders it in an arbitrary position in the DOM. So, given a template like: <template x-ref="source">...</template> I want to be able to render it anywhere with a given data binding using my <template x-template-outlet="$refs.source" x-data="{ foo:"bar" }"></template> This directive does several things:
So, I will wrap the actual DOM-insertion points in the Anyway, I'm still trying to get my head wrapped around the life-cycle stuff. Thanks! |
Beta Was this translation helpful? Give feedback.
-
As an aside, if anyone gets here and is curious to see more about the recursive template rendering, here's a write-up: https://www.bennadel.com/blog/4603-recursive-template-rendering-in-alpine-js-3-13-5.htm |
Beta Was this translation helpful? Give feedback.
Right, yeah I got it mixed up a bit.
since cloning the tree would create those elements that x-for would create, you want to prevent it from initializing x-for again and doubling the elements.
for example.
if you're only mutating a node, it likely won't cause any issues. If you're adding nodes, it might.
mutateDom basically turns off the mutation observer, does your changes, then turns it back on.
This can help ensure that you can control the flow of the initialization of the directives and trees.
So for adding, you might mutateDom( add elements) then initree. For removing, you can do mutateDom ( remove elements) and then destroyTree.
Or rather, destroyTree first?
idk. You destroy the tre…