Search code examples
ember.jsember-dataember-cli

Show the key + values of a model within a foreach loop


I'm using ember 2.1 with ember-data and I'm trying to spit out the keys and values in the ember-data object without having to specify each column individually.

For example:

<ul>
  {{#each model as |row|}}
      <li>{{row}} <small>{{moment-from-now row.createdAt}}</small></li>
  {{/each}}
</ul>

So where it says {{row}} which returns the full object "client@model:modelName::ember939:1". I don't want to have to say {{row.name}} but rather spit out each key and value for each row in the model.

The whole point is to give the component any data from any model, and it will show all the information correctly without having to specify the field names for every db table.


Solution

  • You could use each-in combined with array of attributes of model. I think there should be some better way of getting attributes without using private properties (maybe in controller?), because it is discouraged. However, it's working:

    <ul>
        {{#each model as |row|}}
          {{#each-in row._internalModel._data as |key value|}}
            <li>{{key}} : {{value}}</li>
          {{/each-in}}
          <hr/>
        {{/each}}
    </ul>
    

    Working demo.

    Screenshot:

    screenshot