Ember 数据模型的错误属性 (DS.Errors) 未填充 [英] Ember Data model's errors property (DS.Errors) not populating

查看:17
本文介绍了Ember 数据模型的错误属性 (DS.Errors) 未填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Ember Data,但似乎无法使用来自 REST API 的错误消息填充模型的错误"属性.我几乎遵循本指南中的示例:

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();
            }
        }
    });

我的 API 返回这个:

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."]
  }
}

在模型上调用 save() 后,这是我在用户模型上看到的:

After I call save() on the model, here is what I see on the user model:

user.get('isError') // true
user.get('errors.messages') // []

即使模型正确注册了 isError 属性,我似乎也无法填充错误消息.我怎样才能让它工作?我正在开发 Ember Data 1.0.0-beta.8.2a68c63a 的最新测试版

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.

这是它工作的一个例子,也请查看 Ember:error.messages 在保存时不显示服务器错误,我说同样的话

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

您可以通过覆盖 ajaxError 并复制活动模型适配器如何在 RESTAdapter 上轻松实现它.

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

https://github.com/emberjs/data/blob/v1.0.0-beta.8/packages/activemodel-adapter/lib/system/active_model_adapter.js#L102

这篇关于Ember 数据模型的错误属性 (DS.Errors) 未填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆