保存belongsTo时,Ember数据以字符串形式发送id

时间:2014-03-30 18:46:25

标签: ember.js ember-data promise rsvp-promise

我正在创建一个鸡尾酒应用程序,其模型为“鸡尾酒”,“会员”和“成分”。鸡尾酒和配料模型非常自我解释,会员模型适用于将鸡尾酒与成分相关联的物品。

App.Cocktail = DS.Model.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
  created_by: DS.attr('number'),
  start_date: DS.attr('string'),
  end_date: DS.attr('string'),

  // link cocktail to memberships
  membership: DS.hasMany('membership',{ async: true })
});

App.Membership = DS.Model.extend({
  amount: DS.attr('string'),

  // link membership to ingredient and cocktail
  ingredient: DS.belongsTo('ingredient'),
  cocktail: DS.belongsTo('cocktail')
});

App.Ingredient = DS.Model.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
  start_date: DS.attr('string'),
  end_date: DS.attr('string')
});

我遇到的问题是,当我使用ember-data创建一个鸡尾酒然后它的成分成员时,REST调用的成分和鸡尾酒ID是字符串而不是整数,所以请求体看起来像这样:

{membership:{amount:"2 litres",ingredient:"12",cocktail:"116"}}

当我想要的时候:

{membership:{amount:"2 litres",ingredient:12,cocktail:116}}

这是我的代码执行保存,我对promises的想法很新,所以不确定这是否以最优选的方式构建。

... code
  actions: {
    // fired when user presses save
    submit: function() {
      // some validation

      var cocktailPromise = this._saveCocktail();
      this._saveIngredientMemberships(cocktailPromise);
    }
  }
... more code
  _saveCocktail: function() {
    console.log('saving cocktail');

    var cocktail = this.store.createRecord('cocktail', {
      name: this.get('cocktailName'),
      description: this.get('cocktailDescription'),
      start_date: "now"
    });

    return cocktail.save();
  },

  _saveIngredientMemberships: function(cocktailPromise) {
    console.log('saving cocktail ingredients');

    // get the ingredients and amounts (memberships) the 
    // user entered and the store and set them as var here 
    // so they stay in scope.
    var memberships = this.get('memberships');
    var store = this.store;

    // once cocktail is created
    cocktailPromise.then(function(cocktail) {
      console.log('cocktail ready, saving ingredients');

      var membershipRecords = [];

      for( var i = 0 ; i < memberships.length ; i++ ) {
        membershipRecords[i] = store.createRecord('membership', {
          cocktail: cocktail,
          ingredient: memberships[i].get('ingredient'),
          amount: memberships[i].get('amount')
        });

        membershipRecords[i].save();
      }

    }, function() {
      console.log('something went wrong saving the cocktail!');
    })
  },

1 个答案:

答案 0 :(得分:1)

默认情况下,Ember将所有ID视为字符串(它将数字强制转换为字符串)。您要做的是覆盖RESTSerializer。您可以找到覆盖here的所有可能方法。我建议您也阅读适配器的source code。那真的会有所帮助。我使用Ember-Data已经有一段时间了,但我相信你想看的方法是serialize。我会做这样的事情:

App.ApplicationAdapter = DS.RESTAdapter.extend({
    serialize: function(record, options) {
        var json = this._super(record, options);
        json.ingredient = parseInt(json.ingredient);
        json.cocktail = parseInt(json.cocktail);

        ...

        return json;
    }
});

显然,对于不同的模型,你可以更有效率,但你明白了。您也可以覆盖更具体的方法,这也是我推荐阅读源代码的原因。