Search code examples
javascripttemplate-enginejsrenderjsviews

Data binding in JsViews


I have an array of strings and an object whose properties are derived from this array. For example,

var arrayOne = ["One","Two", "Three"];
var objOne = {};

for(var i=0;i<arrayOne.length;i++){
    objOne[arrayOne[i]] = "some val";
    objOne[arrayOne[i]+"index"]=i;
}

I would like to bind this in the template dynamically like

{^{for arrayOne}}
<input data-link="objOne[:#data]" type="text"/>
{{/for}}

I tried the above approach and a few variations of the same without any luck.Can this be accomplished or am I missing something?


Solution

  • It's probably better not to iterate through the arrayOne array, but instead iterate directly the properties of objOne, along the lines of:

    {^{props objOne}}
      {{if key.slice(-5) !== "index"}}
        <input data-link="prop" type="text"/>
      {{/if}}
    {{/props}}
    

    See http://www.jsviews.com/#jsvpropstag.