2

I have incoming JSON:

[{"key":"browser","value":"Chrome"}, {"key":"geo","value":"false"},{"key":"os","value":"MacOS"}]

And I have to display this by using Handlebars template. I can't use the construction below because **sometimes I have only 2 objects inside JSON:**

Backbone Model
    attr.browser = attr[0];
    attr.geo = attr[1];
    attr.os = attr[2];

Handlebars template:

<ul>
 {{#if browser}}
   <li>{{browser.key}}</li>
   <li>{{browser.value}}</li>
 {{/if}}

 {{#if geo}}
   <li>{{geo.key}}</li>
   <li>{{geo.value}}</li>
 {{/if}}

 {{#if os}}
   <li>{{os.key}}</li>
   <li>{{os.value}}</li>
 {{/if}}
</ul>
Arsenowitch
  • 401
  • 5
  • 22

1 Answers1

1

I found the answer, maybe it will be useful for somebody: So, if you have a Backbone.Model with list of objects inside, like this:

[{"key":"browser","value":"Chrome"}, {"key":"geo","value":"false"},{"key":"os","value":"MacOS"}]

You probably can use this template for display contents of each object:

<ul>
 {{#each this}}
  <li>{{key}}</li>
  <li>{{value}}</li>
 {{/each}}
</ul>
Arsenowitch
  • 401
  • 5
  • 22