ember-data需要完整的对象来保存数据,而不仅仅是id

时间:2015-04-24 06:04:48

标签: ember.js ember-data

我目前遇到的问题是,ember-data需要一个完整的对象来保存。 在我可以保存之前,我需要向商店询问所有完整的对象。 这是我需要做的一个例子。

我添加了我的代码来存储数据和模型。这是正确的做法吗?使用promises会产生大量代码并使所有内容难以阅读。

任何不确定?

亲切的问候 马库斯

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 })
});

1 个答案:

答案 0 :(得分:1)

首先你应该阅读You're missing the point of promises

如果您从.then返回承诺,则承诺可以链接。因此,您的代码可以像这样重写:

this.store.find('fighter', parseInt(this.newRecord.fighter,10)).then(function(data) {
    newRegistration.Fighter = data;
    return controller.store.find('fightRule', parseInt(controller.newRecord.fightRule,10));

}).then(function(data) {
    newRegistration.FightRule = data;
    return controller.store.find('weightClass', parseInt(controller.newRecord.weightClass,10));

}).then(function(data) {
    newRegistration.WeightClass = data;
    return 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);

    return myNewRecord.save();

}).catch(function(reason) {
    // handle errors
});

其次,看起来后来的异步调用不使用前面的异步调用;您只需构建创建新registeredFighter所需的数据。因此,您可以使用Ember.RSVP.hash

在一行代码中提出所有这些请求
var promises = {
    Fighter: this.store.find('fighter', parseInt(this.newRecord.fighter, 10)),
    FightRule: this.store.find('fightRule', parseInt(this.newRecord.fightRule, 10)),
    WeightClass: this.store.find('weightClass', parseInt(this.newRecord.weightClass, 10)),
    FightClass: this.store.find('fightClass', parseInt(this.newRecord.fightClass, 10)),
};

Ember.RSVP.hash(promises).then(function(data) {
    newRegistration.setProperties(data);
    var myNewRecord = controller.store.createRecord('registeredFighter', newRegistration);
    controller.model.event.get('RegisteredFighter').addObject(myNewRecord);

    return myNewRecord.save();
}).catch(function(reason) {
    // handle errors
});

所以,这就是你可以清理异步代码的方法。但它看起来仍然存在设计问题。这很难的原因是因为它不是典型的做事方式:)如果没有更多关于它的信息,很难提供帮助。只知道您可以在新模型上设置关联模型的id来创建它。您不需要所有数据(来自store.find的内容)只是为了保留新记录及其关系。