Skip to content
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

Component fires inserted twice on append. #2288

Closed
taai opened this issue Feb 23, 2016 · 1 comment
Closed

Component fires inserted twice on append. #2288

taai opened this issue Feb 23, 2016 · 1 comment
Milestone

Comments

@taai
Copy link

taai commented Feb 23, 2016

This is probably a duplicate of previously fixed issues like in #1564 , but this is still a problem.

When using components, I want to initialize an instance of component by appending HTML element to the DOM. So, I'm using $('#parent').append(template) instead of $('#parent').html(template). When using .append(), the inserted event of the component gets fired twice. When using .html(), it gets fired once as it should. I don't want to create a separate container just to use .html() for each component instance.

So, my workaround is this:

$('#parent').append(
    $(document.createDocumentFragment()).html(can.stache('<my-component></my-component>'))
);

Because .html() works - see that in #1576

I made a JSFiddle with a problem:
http://jsfiddle.net/kmqys2fw/3/

By the way, in the example I'm using jQuery 2.1.4. I noticed that when using jQuery 2.2.x the inserted trigger does not get triggered at all when using .append(), and my workaround does not work at all.

@daffl daffl added this to the 2.3.18 milestone Feb 23, 2016
@daffl daffl modified the milestones: 2.3.19, 2.3.18 Mar 3, 2016
@justinbmeyer
Copy link
Contributor

This doesn't seem like a bug. can.stache returns a function, you need to call it like:

can.stache('<my-component></my-component>')()

Example:

http://jsfiddle.net/kmqys2fw/4/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants