vendredi 30 septembre 2016

template won't render from model object after sorting

I am sorting an array of objects queried from Ember-Data by 'type'-key before returning them in the model()-method of an Ember.Route to be rendered.

app/routes/test.js

export default Ember.Route.extend({
  model() {
    let obj = Ember.Object.create({a: [], b: [], c: []});
    this.get('store').findAll('obj').then(function(unsorted){
      unsorted.forEach(function(item) {// sort per type
        obj.get(item.get('type')).addObject(item);
      });
      return obj;
    });
  }
});

The array returned by (the promise of) the Ember-Data query looks like this (all objects have Ember internal properties)

[
  {
    _id: '1',
    type: 'a',
    properties: {...}
  },
  {
    ...
  },
  {
    _id: '15',
    type: 'b',
    properties: {...}
  }
]

And the new object with objects sorted by "type"-key looks like this

{
  a: [{
      _id: '1',
      type: 'a',
      properties: {...}
    },
    ...
  ],
  b: [
    ... ,
    {
      _id: '15',
      type: 'b',
      properties: {...}
    },
  c: [...]
};

app/routes/test.hbs

<h2>Test</h2>
<h3>Type a</h3>

  <div>
    
  </div>


The template doesn't render the part that loops over the array, nor does the Ember-inspector plugin list the Model property under "Own properties" of the route (right panel in "View tree"-mode).

However, when returning a POJO (literally pasting object with array's by key in code) everything behaves as expected.
I suspect this has something to do with the internal (Ember-)properties of the objects returned by Ember-data (I have read about the owner of an object etc.) but I can't seem to figure this out ...




Aucun commentaire:

Enregistrer un commentaire