Ember数据模型的错误属性(DS.Errors)不填充 [英] Ember Data model's errors property (DS.Errors) not populating
问题描述
我正在使用Ember Data,我似乎无法让模型的'errors'属性填充来自我的REST API的错误消息。我非常遵循本指南的例子:
http://emberjs.com/api/data/classes/DS.Errors.html
我的应用如下所示:
window.App = Ember.Application.create();
App.User = DS.Model.extend({
用户名:DS.attr('string'),
电子邮件:DS.attr('string')
});
App.ApplicationRoute = Ember.Route.extend({
model:function(){
return this.store.createRecord('user',{
username :'mike',
电子邮件:'invalidEmail'
});
},
操作:{
save:function(){
this.modelFor(this.routeName).save();
}
}
});
我的API返回:
HTTP / 1.1 400错误请求
内容类型:application / json; charset = utf-8
内容长度:125
{
errors:{
username:[用户名被占用! b $ b电子邮件:[电子邮件无效]
}
}
在模型上调用save()之后,这里是我在用户模型中看到的:
user。 get('isError')// true
user.get('errors.messages')// []
即使模型正确地注册了isError属性,我似乎无法获取错误消息来填充。我该如何让这个工作?我正在研究Ember Data 1.0.0-beta.8.2a68c63a的最新beta版本。在这方面缺乏这些错误,除非你使用了活动的模型适配器,否则这些错误是不会被填充的。
这里是一个例子,也可以查看Ember:error.messages不显示保存时的服务器错误我说同样的事情
http://jsbin.com/motuvaye/24/edit
您可以通过覆盖在RESTAdapter上轻松实现它ajaxError
并复制活动模型适配器的操作方式。
App.ApplicationAdapter = DS.RESTAdapter。 extend({
ajaxError:function(jqXHR){
var error = this._super(jqXHR) ;
if(jqXHR&&& jqXHR.status === 422){
var response = Ember。$。parseJSON(jqXHR.responseText),
errors = {};
if(response.errors!== undefined){
var jsonErrors = response.errors;
Ember.EnumerableUtils.forEach(Ember.keys(jsonErrors),function(key){
errors [Ember.String.camelize(key)] = jsonErrors [key] ;
});
}
返回新的DS.InvalidError(errors);
} else {
return error;
}
}
});
http://jsbin.com/motuvaye/27/edit
I'm using Ember Data and I can't seem to get the model's 'errors' property to populate with the error messages from my REST API. I'm pretty much following the example at this guide:
http://emberjs.com/api/data/classes/DS.Errors.html
My app looks like this:
window.App = Ember.Application.create();
App.User = DS.Model.extend({
username: DS.attr('string'),
email: DS.attr('string')
});
App.ApplicationRoute = Ember.Route.extend({
model: function () {
return this.store.createRecord('user', {
username: 'mike',
email: 'invalidEmail'
});
},
actions: {
save: function () {
this.modelFor(this.routeName).save();
}
}
});
And my API returns this:
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Content-Length: 125
{
"errors": {
"username": ["Username is taken!"],
"email": ["Email is invalid."]
}
}
After I call save() on the model, here is what I see on the user model:
user.get('isError') // true
user.get('errors.messages') // []
Even though the model is registering the isError property correctly, I can't seem to get the error messages to populate. How can I get this to work? I'm working on the latest beta build of Ember Data version 1.0.0-beta.8.2a68c63a
The docs are definitely lacking in this area, the errors aren't populated unless you're using the active model adapter.
Here's an example of it working, also check out Ember: error.messages does not show server errors on save where I say the same thing
http://jsbin.com/motuvaye/24/edit
You can fairly easily implement it on the RESTAdapter by overriding ajaxError
and copying how the active model adapter does it.
App.ApplicationAdapter = DS.RESTAdapter.extend({
ajaxError: function(jqXHR) {
var error = this._super(jqXHR);
if (jqXHR && jqXHR.status === 422) {
var response = Ember.$.parseJSON(jqXHR.responseText),
errors = {};
if (response.errors !== undefined) {
var jsonErrors = response.errors;
Ember.EnumerableUtils.forEach(Ember.keys(jsonErrors), function(key) {
errors[Ember.String.camelize(key)] = jsonErrors[key];
});
}
return new DS.InvalidError(errors);
} else {
return error;
}
}
});
http://jsbin.com/motuvaye/27/edit
这篇关于Ember数据模型的错误属性(DS.Errors)不填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!