jeudi 23 avril 2015

ember-data needs full object for saving data not only the id

i currently have the problem, that ember-data needs a full object to get saved. Before i can save i need to ask the store for all the full objects. here is a example of what i need to do.

I added my code to store data and the models. Is this the correct way of doing this? Working with promises produces always a lot of code and makes everything hard to read.

Any indeas?

Kind Regards Markus

this.store.find('fighter', parseInt(this.newRecord.fighter,10)).then(function(data) {
    newRegistration.Fighter = data;
    controller.store.find('fightRule', parseInt(controller.newRecord.fightRule,10)).then(function(data) {
        newRegistration.FightRule = data;
        controller.store.find('weightClass', parseInt(controller.newRecord.weightClass,10)).then(function(data) {
            newRegistration.WeightClass = data;
            controller.store.find('fightClass', parseInt(controller.newRecord.fightClass,10)).then(function(data) {
                newRegistration.FightClass = data;
                var myNewRecord = controller.store.createRecord('registeredFighter', newRegistration);
                controller.model.event.get('RegisteredFighter').addObject(myNewRecord);
                myNewRecord.save();
            });
        });
    });
});

------------------------- MODELS --------------------------------

App.Fighter = DS.Model.extend({
  Name:  DS.attr('string'),
  Club:  DS.belongsTo('club', { async: true }),
  Birthday:  DS.attr('date'),
  MembershipNr:  DS.attr('number')
});

App.Event = DS.Model.extend({
  Name:  DS.attr('string'),
  Thumbnail:  DS.attr('string'),
  Ort:  DS.attr('string'),
  Contact:  DS.attr('string'),
  RegisteredFighter: DS.hasMany('registeredFighter', { async: true })
});

App.Club = DS.Model.extend({
  Name:  DS.attr('string'),
  Contact:  DS.attr('string'),
});

App.FightRule = DS.Model.extend({
  Name:  DS.attr('string')
});

App.WeightClass = DS.Model.extend({
  Name:  DS.attr('string')
});

App.FightClass = DS.Model.extend({
  Name:  DS.attr('string'),
  TabName: function() {
    return 'tab' + this.get('Name').underscore().capitalize();
  }.property('Name')
});

App.User = DS.Model.extend({
  Name:  DS.attr('string'),
  AllowFighter:  DS.attr('boolean'),
  AllowEvents: DS.attr('boolean'),
  IsAdmin: DS.attr('boolean')
});

App.RegisteredFighter = DS.Model.extend({
  Fighter:  DS.belongsTo('fighter', { async: true }),
  FightRule:  DS.belongsTo('fightRule', { async: true }),
  WeightClass: DS.belongsTo('weightClass', { async: true }),
  FightClass: DS.belongsTo('fightClass', { async: true })
});




Aucun commentaire:

Enregistrer un commentaire