New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<m-insert> inside m-for #171
Comments
I think i know the cause: in https://github.com/kbrsh/moon/blob/master/src/util/dom.js#L36
should be:
because
so when |
Huh, thanks for letting me know! The issue seems to be with Other than that, multiple inserts aren't supported, and I'm not planning on adding them. It's because each insert refers to the same set of elements, and Moon can only bind them to one DOM node. As a result, it will break when diffing. |
What do you mean by if so, do you have any suggestion on how to achieve multiple inserts without relying on Moon ? My intention is to build a List component, so the user can put each item template directly in their component. Something like:
I am thinking of cloning instance.insert, convert it into html, modify user's component template, and then create a new component with the new template ... is it a good idea ? Any suggestion ? |
Yup, you cannot have multiple How do you think that list component will work? Modifying the component template won't work either. Moon templates are compiled—for example: <div class="component">
<m-insert/>
</div> is compiled into: var compiledRender = function(m) {
var instance = this;
var staticNodes = instance.compiledRender.staticNodes;
if(staticNodes === undefined) {
staticNodes = instance.compiledRender.staticNodes = [];
}
return m("div", {attrs: {"class": "component"}}, {}, m.flatten([instance.insert]));
} |
Let say we have this code for displaying a list: <div m-for="item in [1,2,3,4]">
<b>{{ item }}</b> <!-- list item detail -->
</div> The requirement of my component is like this: <list data="{{ [1,2,3,4] }}">
<b>{{ item }}</b> <!-- list item detail -->
</list> I know the Now.... What if we can pre-process that template just before it being compiled by Moon. <list>
<div m-for="item in [1,2,3,4]">
<b>{{ item }}</b> <!-- list item detail -->
</div>
</list> User doesn't need to know The use case for this is, I want to have a powerful If we can pre-process the template, then we can rewrite it's structure and get it's children overwritten. My point is the user does not need to know them to use my |
Well I just did it in server side, before each component sent to client:
Now, my or not... I don't know, what`s the catch ? |
I think it's a cool idea, but a little hacky. Instead of having it be preprocessed, I'd recommend a component with an API like: <List>
<div m-for="item in items"></div>
</List> See, slots are bound to the parent, so the You can manipulate the DOM as well. Each component (after being mounted) has access to |
Ah, I think this.root solves it! |
It would be nice if Moon gives a warning when user try to put multiple inserts |
Cool! I'll try and add a warning, so for now I can keep this issue open. |
Updating It's mostly so you can set stuff like the height and width of the element. You can also access the |
Oops, sorry i just created new issue regarding that question. |
Adding a warning for multiple slots is actually more complex than I initially thought. It would require a getter to see if it has been accessed multiple times and would require code in the render function or code generator. I don't think it's that big of an issue though, so I'm closing it for now. Let me know if you have any thoughts on this. |
I just tried this:
and then:
The text was updated successfully, but these errors were encountered: