【问题标题】:Ember data sending ids as strings when saving belongsToEmber 数据在保存时将 id 作为字符串发送
【发布时间】:2014-03-30 18:46:24
【问题描述】:

我正在使用模型“鸡尾酒”、“会员”和“成分”创建一个鸡尾酒应用程序。鸡尾酒和配料模型很容易解释,而会员模型适用于将鸡尾酒与配料和数量联系起来的对象。

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

这是我执行保存的代码,我对 Promise 的想法很陌生,所以不确定这是否以最可取的方式构建。

... 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!');
    })
  },

【问题讨论】:

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


    【解决方案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;
        }
    });
    

    显然,您可以针对不同的模型提高效率,但您明白了。您也许还可以覆盖更具体的方法,这也是我建议阅读源代码的原因。

    【讨论】:

    • 非常感谢,这是一种享受,是的,多看一下 REST 适配器源肯定会帮助我:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 2023-02-07
    • 1970-01-01
    • 2011-04-30
    相关资源
    最近更新 更多