Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymer 1.0 - injectBoundHTML() alternative

What's the Polymer 1.0 equivalent to injectBoundHTML()?

(i.e. appending HTML strings to nodes within a Polymer element and having data bindings resolve)

A JSbin example - http://jsbin.com/jufase/edit?html,output

EDIT: don't have enough SO cred to accept my own answer yet, but it should be down below somewhere. TL;DR - use "dom-bind" templates

like image 722
techknowledgey Avatar asked Jun 15 '15 01:06

techknowledgey


1 Answers

Although as techknowledgey pointed out it's not really supported well yet. The following seems to do the trick.

function injectBoundHTML(html, element) {
    var template = document.createElement('template', 'dom-bind');
    var doc = template.content.ownerDocument;
    var div = doc.createElement('div');
    div.innerHTML = html;
    template.content.appendChild(div);
    while (element.firstChild) {
        element.removeChild(element.firstChild);
    }
    element.appendChild(Polymer.Base.instanceTemplate(template));
}

If your HTML was already parsed then use something like "doc.importNode(sourceNode, true);" instead of getting/setting innerHTML.

like image 73
Mark Doyle Avatar answered Oct 02 '22 13:10

Mark Doyle