I'm using Ember/Ember-Data v.2.1.0 to develop an analytics webapp, and new to both...
My route loads a model 'parent' which has a hasMany relationship with another model 'child', each of which has several belongsTo relationships with a third model 'grandchild'. It is not unusual for a parent to have a dozen children, each of which has a dozen children, each of which displayed in the template and consumed by data-oriented components.
One of the properties on 'grandchild' is a hash with key/value pairs.
ratings: DS.attr()
Which in the database is...
{ 'r1': 10, 'r2': 30, 'r3': 35... }
Another property on 'grandchild' is a computed property that returns a specific value in the hash, and a third property which does some math...
rating: Ember.computed('ratings', 'rSample', function() {
return this.get(`ratings.${this.get('rSample')}`);
},
cost: DS.attr(number),
value: Ember.computed('rating', 'cost', function() {
return this.get('rating') / this.get('cost');
}
This works; however, what I want is to have 'rSample' as a value on the route's controller.
To goal is to have a component in the route's template that allows the user to set the value of 'rSample', which will be consistent across all 'grandchild' records displayed in the template and used in the analytics components.
Since none of these computed properties is ever saved to the database, I'm not sure the model is even the correct place for these computed properties. And having a model be aware of a controller seems like an anti-pattern.
But I have no idea how to set computed properties for the grandchildren on the controller, since the route is loading the 'grandparent' model.
Aucun commentaire:
Enregistrer un commentaire