jsrender

JSRender unable to data-link in subtemplate


I am new in using JS-render, I copied the demo code in JSrender Demo site, and added input data-link="name", but I found the "name" could not shown in the textbox.

Could anyone can help? Many thanks.

Best Rgds


<script id="columnTemplate" type="text/x-jsrender">
  <div>
    <em>{{:name}}</em>
    <input data-link="name" />
  </div>
</script>

<script id="rowTemplate" type="text/x-jsrender">
  <span>
    <b>{{:name}}</b>
    <input data-link="name" />
  </span>
</script>


var movies = [
  {
    title: "Meet Joe Black",
    tmpl: "columnTemplate",
    languages: [
      {name: "English"},
      {name: "French"}
    ]
  },
  {
    title: "Eyes Wide Shut",
    tmpl: "rowTemplate",
    languages: [
      {name: "French"},
      {name: "Esperanto"},
      {name: "Spanish"}
    ]
  }

Solution

  • You are wanting to add dynamic two-way data-binding (data-linking) to a JsRender template. To do that you need to use jsviews.js and not jsrender.js.

    Take a look at the introductory Playing with JsViews, and you will see how to convert a JsRender sample to a data-linked (dynamic) JsViews sample. Read up to the two-way data binding sample, for example.

    You JsViews version of the Composition, using contextual template objects sample will end with this code:

    var movieTemplate = $.templates("#movieTemplate");
    
    var nestedTemplates = {
      columnTemplate: $.templates("#columnTemplate"),
      rowTemplate: $.templates("#rowTemplate"),
      conditionalTemplate: $.templates("#conditionalTemplate")
    };
    
    movieTemplate.link("#movieList", movies, nestedTemplates);
    

    which replaces the following original code from the JsRender (static template rendering) sample:

    var movieTemplate = $.templates("#movieTemplate");
    
    var nestedTemplates = {
      columnTemplate: $.templates("#columnTemplate"),
      rowTemplate: $.templates("#rowTemplate"),
      conditionalTemplate: $.templates("#conditionalTemplate")
    };
    
    var html = movieTemplate.render(movies, nestedTemplates);
    
    $("#movieList").html(html);
    

    Also, in the template itself, you need to specify where you want data-linked tags. For example, replace {{>name}} with {^{>name}}.