【问题标题】:Ember Data 1.0.0: Model name capitalized in URLEmber Data 1.0.0:在 URL 中大写的模型名称
【发布时间】:2013-09-02 13:56:07
【问题描述】:

我正在从 Ember Data 0.13 过渡到 Ember Data 1.0(测试版 1)。似乎为模型构建的 URL 在不应该大写的情况下大写。在 ED 0.13 中,大写和复数自动发生并且没有问题。我想在 ED 1.0 中也是如此,但我肯定忽略了一些东西。

App.Account = DS.Model.extend({
  // Attributes
  company: DS.attr('string'),

  // Relationships
  users: DS.hasMany('User')
});

App.AccountAdapter = DS.RESTAdapter.extend({
  namespace: 'api',
});

在控制器中,我创建一个新记录,填充并保存它。

var account = this.store.createRecord('account');
account.set('company', this.get('company'));
account.save();

Ember Data 用于保存记录的请求 URL 是 http://localhost:3000/api/Accounts为什么型号名称的复数要大写?如何将模型/适配器配置为使用 accounts 而不是 Accounts

【问题讨论】:

  • 这似乎没有发生在 ED 1.0.0 的 find() 中。测试版 14.1。 type.typekey 正在从类中生成一个小写字符串。在您的情况下为“帐户”。

标签: ember.js ember-data


【解决方案1】:

看来模型关联命名约定的更改是导致此问题的原因。在 ED 1.0 之前,关联声明如下所示。

App.Account = DS.Model.extend({
  // Attributes
  company: DS.attr('string'),

  // Relationships
  users: DS.hasMany('App.User')
});

然而,在 ED 1.0 中,不需要使用 App.User 来声明关联。传递模型的名称,user 就足够了。因为我在User 模型中将Account 模型的名称大写,所以 ED 在 URL 中将模型的名称(复数)大写。

App.User = DS.Model.extend({
  // Attributes
  first_name: DS.attr('string'),
  last_name: DS.attr('string'),
  email: DS.attr('string'),
  password: DS.attr('string'),
  password_confirmation: DS.attr('string'),

  // Relationships
  account: DS.belongsTo('account') // model name should be lowercase
});

就像您需要传递模型的小写名称来创建新记录(this.store.createRecord('user');)一样,您还需要使用小写的模型名称来指定关联的模型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多