2
votes

In Polymer 0.5 I used the templateElement.createInstance(dataToBind) to create a new instance of a template and bind the data object to the new instance:

var instance = templateElement.createInstance(dataToBind);
container.appendChild(instance);

In Polymer.Base I found the instanceTemplate function to create a new instance of the template, but this function does not bind the data to the instance. Is there a way to achieve this in Polymer 1.0 ?

1

1 Answers

4
votes

If you define a template like this:

<template is="dom-template">
  <h2>{{name}}</h2>
  <h3>...lives<h3>
</template>

you can generate an instance like this:

<script>
  // construct the anonymous presenter
  var instance = templateInstance.stamp();
  // the data model lives on the presenter
  instance.name = 'Instance';
  // the nodes are available in `root`
  document.body.appendChild(instance.root);
</script>

This is more or less what happens when you instance a Polymer element, except the presenter is the instance of your element (instead of an anonymous object).