2

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

Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
kilua
  • 711
  • 1
  • 9
  • 16

1 Answers1

1

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:
apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • hi Thanks for the reply, that **of** does work inside the backbone-view. But it does not work in template.jst.eco. – kilua Oct 30 '13 at 08:56
  • seems, the **template.jst.eco** does not recognize the **of**. Any idea? Thanks – kilua Oct 30 '13 at 08:58
  • I think it stems from the fact you have an array then an object so you must loop the single element array THEN loop the object – apneadiving Oct 30 '13 at 08:59
  • hmm i dont really get it, so is it kind of nested loop ? Could you help me out with sample. thank you very much – kilua Oct 30 '13 at 09:02
  • I got this error, **Uncaught TypeError: Cannot read property 'length' of undefined** I think its from the `for object in @data_transaction.models:` – kilua Oct 30 '13 at 09:06