dimanche 16 juillet 2017

Is there a way to check if there is existing record before creating new one?

-So, first of all, I want to explain models which I have. Class has many subjects and many students. One student belongs to one class and has many grades. One subjects belongs to one class and has many grades. One grade belongs to one subject and one student.

-Is there a way where I can in controller first check if there is an existing grade with that student and that subject before I create grade record. Because if there is such record I only want to update marks property. Can somebody give me an example of implementation in answer / comment?

-I will leave my models here:

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({
        ocene: DS.attr('array'),
        subject: DS.belongsTo('subject'),
        student: DS.belongsTo('student')
});

app/models/grade.js:

import DS from 'ember-data';

export default DS.Model.extend({
        marks: DS.attr('array'),
        subject: DS.belongsTo('subject'),
        student: DS.belongsTo('student')
});

-And here is an array type that I made for marks property in grade models (app/transforms/array.js):

import Ember from 'ember';
import DS from 'ember-data';

export default DS.Transform.extend({
        deserialize: function(serialized) {
                if (Ember.isArray(serialized)) {
                        return Ember.A(serialized);
                } else {
                        return Ember.A();
                }
        },

        serialize: function(deserialized) {
                if (Ember.isArray(deserialized)) {
                        return Ember.A(deserialized);
                } else {
                        return Ember.A();
                }
        }
});



Aucun commentaire:

Enregistrer un commentaire