2

I'm attempting to be able to import html fragments into my code as follows:

var links = document.querySelectorAll('link[rel="import"]');
fragments = {};
for (var i = 0; i < links.length; i++) {
    var link = links.item(i);
    if (definesFragment(link)) { // returns whether or not the import defines a valid fragment
        var name = getTemplateName(link); // returns the file name from the link without the path or extension
        var proto = Object.create(HTMLElement.prototype);
        proto.createdCallback = function() {
            var fragment = link.import.querySelector('fragment');
            var clone = document.importNode(fragment, true);
            var root = this.createShadowRoot();
            root.appendChild(clone);
        };
        fragments[name] = document.registerElement('foo-' + name, { prototype: proto });
        //console.log(new fragments[name]())
    }
}

and then if I want to add this fragment to the page, I would write:

var frag = new fragments.bar();
document.body.appendChild(frag);

It seems to work correctly if there is only one fragment file defined, but as soon as there are multiple links, it modifies all previously registered elements to have the new fragment as their body.

For example, if I have two files:

bar.html:

<fragment>
    bar
</fragment>

baz.html:

<fragment>
    baz
</fragment>

If I un-comment the console.log at the bottom of the first script, it prints:

<foo-bar>
    #shadowroot {
        <fragment>
            bar
        </fragment>
    }
</foo-bar>

<foo-baz>
    #shadowroot {
        <fragment>
            baz
        </fragment>
    }
</foo-baz>

but if bar is included before baz, when I console.log(new fragments.bar()) later in the code it logs:

<foo-bar>
    #shadowroot {
        <fragment>
            baz
        </fragment>
    }
</foo-bar>

I am curious as to why the previously registered elements are modified when I register another.

Thanks, Jake

Chrums
  • 45
  • 4
  • The is never registered using registerElement, it is simply an element to be searched for using querySelector('fragment'). if I were to create a baz element using document.createElement it would need to be document.createElement('foo-baz') – Chrums May 12 '15 at 21:57
  • Each individual html file being imported should only have one tag in it. – Chrums May 12 '15 at 22:00
  • The import link tag is working. If there are two files (bar and baz) that have been included in the html as My script finds both of them and the length of links = 2. It also finds the names appropriately, and if I immediately console.log(new fragments[name]()) after defining it, it prints out the appropriate element. The issue is after registering the second one, the first one changes to have a shadowroot that is the fragment from the second one. – Chrums May 12 '15 at 22:11
  • hmm, i would re-post with the added info(and remove the non-essentials) and a `web-components` tag – dandavis May 12 '15 at 22:28
  • 1
    From a quick glance, this seems to be a dupe of http://stackoverflow.com/q/750486/1331430 – Fabrício Matté May 12 '15 at 23:04
  • I agree with Fabricio. Check this is not a closure problem. – Bob May 12 '15 at 23:08
  • It was indeed a duplicate of that! Thanks! – Chrums May 12 '15 at 23:10

0 Answers0