So, this thread is just a continuation of previous topic (Always getting not null object even though there are no objects with these parameters; - #10 by komi94).
Basically everything will be clear what the bag is from screenshots that I will leave. Also I will leave snippets of models, template and controller.
-First insertion of the grade:
-Second insertion of the grade (from different subject):
-Models:
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')
});
-Template:
<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>
{{#if showS}}
<br> Current subjects in class:
{{#each model.subjects as |subject|}}
<br> <b> {{subject.title}} </b>
Choose grade:
<select onchange={{action (mut ocena) 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 'unesiOcenu' student.id subject.id}}> Insert mark for student </button>
{{/each}}
{{#each student.grades as |grades|}}
<table>
<th> {{grades.subject.title}} </th>
<th> {{grades.marks}} </th>
</table>
{{/each}}
{{/if}}
{{else}}
<b> Currently there are no students for the class! </b>
{{/each}}
</ol>
</table>
<button class="choose" {{action 'showSubjects'}}> Show subjects </button>
</center>
-Controller:
findGradesFor: function(student, subject){
var grades = this.store.peekAll('grade');
grades.filter((grade) =>{
return grade.get('student') === student && grade.get('subject') === subject;
});
return grades.get('length') ? grades : null;
},
and action:
unesiOcenu(student, subject){
var mark = this.get('ocena');
console.log(student.get('firstName') + "" + student.get('lastName'));
console.log(subject.get('title'));
var filteredGrades = this.findGradesFor(student, subject);
if(filteredGrades.get('firstObject')){
filteredGrades.get('firstObject').get('marks').pushObject(mark);
filteredGrades.save();
}else{
this.store.createRecord('grade', {
marks: [mark],
student: student,
subject: subject
}).save().then(function(ocena){
student.get('grades').pushObject(ocena);
student.save();
subject.get('grades').pushObject(ocena);
subject.save();
});
}
}