Overall, I generally like Ember and Ember data but sometimes I want to pull my hair our when accessing the manyArrays for hasMany relationships. For example, I have models where a Student hasMany Lessons, loaded async (getting it to sideload properly is a whole other nightmare). This all hooks together fine, but when attempting to iterate over this, I run into so much .content
nesting I fear I must be doing something wrong:
var = lessons = this.get('lessons'); // model is a student
Only returns one object. In order to get to the array I must fetch two levels deeper: this.get('lessons.content.content')
. This seems non-idiomatic. And even after going that deep, and finding the actual array, each element in the array is another mess without my data in it. I believe this has something to do with Promises not having resolved but I feel this use case is incredible simple and common so can anyone see what I’m missing? Even if I wrap the code as this.get('lessons').then(...)
I get an error that lessons
is undefined as my model has fulfilled yet. Isn’t Ember supposed to deal with this asynchrony?
// bower.json
"ember": "1.8.1",
"ember-data": "1.0.0-beta.12",
// models/school.js
import DS from 'ember-data';
import EmberValidations from 'ember-validations';
import Entity from './entity';
export default Entity.extend(EmberValidations.Mixin, {
name: DS.attr('string'),
info: DS.attr('string'),
students: DS.hasMany('student', { async: true }),
});
// models/student.js
import DS from 'ember-data';
import Entity from './entity';
export default Entity.extend({
name: DS.attr('string'),
email: DS.attr('string'),
picture: DS.attr('string'),
lessons: DS.hasMany('lesson', { async: true }),
school: DS.belongsTo('school', { async: true }),
});
// models/lesson.js
import Ember from 'ember';
import DS from 'ember-data';
import EmberValidations from 'ember-validations';
export default DS.Model.extend(EmberValidations.Mixin, {
name: Ember.computed('lessonType', function() {
return ["Introductions", "Educational Culture", "Etiquette", "Public Speaking", "Residential and Social life", "Excelling in the U.S. System", "Teamwork", "Planning & Transition", "Time & Resources Management", "Long-Term Aspirations", "Shearwater Elective & Final Planning"][this.get('lessonType') - 1];
}),
lessonType: DS.attr('number'),
score: DS.attr('number'),
student: DS.belongsTo('student', { async: true }),
});
import DS from 'ember-data';
export default DS.Model.extend({
user: DS.belongsTo('user', { async: true })
});