Rails Backbone Render Nested JSON in jst.eco template

506 Views Asked by At

I have this set JSON data

JSON

[{
    "country": {
        "name": "Malaysia",
        "total_amount": 0.0,
        "count": 0
    }
}, {
    "country": {
        "name": "Philippines",
        "total_amount": 0.0,
        "count": 0
    }
}, {
    "country": {
        "name": "Thailand",
        "total_amount": 0.0,
        "count": 0
    }
}]

Let say this data I sent it like

.replaceWith(@template(data: @data_transaction) 

How do I retrieve the data inside the template.jst.eco

I have tried to use this kind of for loop

<% for key, data in @data_transaction.models: %>
   <%= data.get("country").name %>
   <%= data.get("country").total_amount %>
   <%= data.get("country").count %>

it just does not work

if I tried to print it out this way inside the template.jst.eco

<%= @data_transaction %>

it will show this kind of things

[object, object]

Any helps?

Thank you very much

1

There are 1 best solutions below

5
On

I fear you made a very common mistake.

Try to replace:

for key, data in @data_transaction.models:

with:

for object in @data_transaction.models:
  for key, data of object: