mardi 4 octobre 2016

Ember async hasMany sorting undefined properties

I'm using Ember v2.8 with firebase, and am having trouble with async loading of a hasMany relationship.

Model1

export default DS.Model.extend({
  model2s: DS.hasMany('model2'),
  sort: [ 'property1:asc', 'property2:asc' ],
  sortedModel2s: Ember.computed.sort('model2s', 'sort'),
  reducer(result, model2) {
    // Would do something interesting, but
    // model2.get('property1') === undefined :(
  },
  somethingInteresting: Ember.computed('sortedModel2s.@each', function() {
    return this.get('sortedModels2').reduce(this.reducer, 1);
  })
})

I would expect somethingInteresting to get computed when there is actually data in the sorted model, but instead it gets an array with the right number of objects, but each with empty properties. The only property on each model2 is id. It then will fetch all the model2s and compute again every time (this is really expensive with hundreds of models).

The workaround I'm using is this, but it feels like the wrong way to go about it, especially since it wouldn't really update with changes:

realSortedModel2s: Ember.computed(function() {
  this.get('model2s').then(models => this.set('realSortedModel2s', models));
  return [];
});

I'd much rather have sortedModel2s return a promise if it's just going to be filled with empty values, and then I can have somethingInteresting calculate using the promise, but maybe I'm understanding the situtation incorrectly.




Aucun commentaire:

Enregistrer un commentaire