Before I give any snippets of code, I want to explain the task that I have to implement.
First of all, every class has many subjects and every class has many students. Every subject and student belongs to one class. Also, every subject and student has many grades. But one grade belongs to one subject and one student.
I wanted first in controller to check if there is one grade with those parameters (student and subject), and then if there is that grade, just update marks property (type of ‘array’ which I have in app/transforms). So here is controller app/controllers/overviewofstudents.js:
import Ember from 'ember';
export default Ember.Controller.extend({
showS: false,
mark: "",
actions:{
showSubjects(){
this.toggleProperty('showS');
},
insertMark(val, val2){
var subject2 = this.get('store').peekRecord('subject', val2);
var student = this.get('store').peekRecord('student', val);
var mark2 = this.get('mark');
this.get('store').query('grade', {
filter: {
student: student,
subject: subject2
}
}).then(function(mark){
if(mark == null){
this.get('store').createRecord('grade', {
marks: [mark2],
student: student,
subject: subject2
}).save().then(function(grade){
student.get('marks').pushObject(mark2);
student.save();
subject2.get('grades').pushObject(mark2);
subject2.save();
});
}else{
mark.get('marks').pushObject(mark2);
mark.save();
}
});
}
}
});
And here is app/templates/overviewofstudents.hbs:
<br> <br> <br> <br> <br>
<center>
<table>
<h2> Chosen class: {{model.name}} </h2>
<br>
<i> Current students in class: </i> <br>
<ol>
{{#each model.students as |student|}}
<li> {{student.firstName}} {{student.lastName}} </li>
<button class="choose" {{action 'showSubjects'}}> Show subjects </button>
{{#if showS}}
<br> Current subjects in class:
{{#each model.subjects as |subject|}}
<br> <b> {{subject.title}} </b>
Choose grade:
<select onchange={{action (mut mark) value="target.value"}}>
<option value="1"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 3 </option>
<option value="4"> 4 </option>
<option value="5"> 5 </option>
</select>
<br>
<button {{action 'insertMark' student.id subject.id}}> Insert mark for student </button>
{{/each}}
{{/if}}
{{else}}
<b> Currently there are no students for the class! </b>
{{/each}}
</ol>
</table>
</center>
-And all I get is error: TypeError: Cannot read property ‘pushObject’ of undefined
-I will also leave models for class, student, subject and grade.
app/models/class.js:
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
subjects: DS.hasMany('subject'),
students: DS.hasMany('student')
});
app/models/student.js:
import DS from 'ember-data';
export default DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
grades: DS.hasMany('grade'),
class: DS.belongsTo('class')
});
app/models/subject.js:
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
grades: DS.hasMany('grade'),
class: DS.belongsTo('class')
});
app/models/grade.js:
import DS from 'ember-data';
export default DS.Model.extend({
ocene: DS.attr('array'),
subject: DS.belongsTo('subject'),
student: DS.belongsTo('student')
});