我正在从Ember Data 0.13过渡到Ember Data 1.0(beta 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
?
答案 0 :(得分:0)
模型关联的命名约定似乎是导致此问题的原因。在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
就足够了。因为我在Account
模型中将User
模型的名称大写,所以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');
)一样,您还需要使用小写模型名称来指定关联的模型。