使用 Ember.js 数据 RESTAdapter 时应该如何处理错误? [英] How should errors be handled when using the Ember.js Data RESTAdapter?

查看:14
本文介绍了使用 Ember.js 数据 RESTAdapter 时应该如何处理错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ember-data.js:https://github.com/emberjs/data/tree/0396411e39df96c8506de3182c81414c1d0eb981

ember-data.js: https://github.com/emberjs/data/tree/0396411e39df96c8506de3182c81414c1d0eb981

简而言之,当出现错误时,我想在视图中显示错误信息,然后用户可以 1)取消,这将回滚事务 2)纠正输入错误并成功提交事务,通过服务器上的验证.

In short, when there is an error, I want to display error messages in the view, and then the user can 1) cancel, which will rollback the transaction 2) correct the input errors and successfully commit the transaction, passing the validations on the server.

以下是源代码片段.它不包括错误回调.

Below is a code snippet from the source. It doesn't include an error callback.

updateRecord: function(store, type, record) {
  var id = get(record, 'id');
  var root = this.rootForType(type);

  var data = {};
  data[root] = this.toJSON(record);

  this.ajax(this.buildURL(root, id), "PUT", {
    data: data,
    context: this,
    success: function(json) {
      this.didUpdateRecord(store, type, record, json);
    }
  });
},

总的来说,从服务器接收错误并更新视图的流程是什么?似乎错误回调应该将模型置于 isError 状态,然后视图可以显示适当的消息.此外,事务应该保持脏.这样,事务就可以使用rollback.

Overall, what is the flow of receiving an error from the server and updating the view? It seems that an error callback should put the model in an isError state, and then the view can display the appropriate messages. Also, the transaction should stay dirty. That way, the transaction can use rollback.

不过,似乎使用 store.recordWasInvalid 的方向是正确的.

It seems that using store.recordWasInvalid is going in the right direction, though.

推荐答案

这个周末我试图解决同样的问题.按照 Luke 所说,我仔细查看了 最新提交(12 月11).

This weekend I was trying to figure the same thing out. Going off what Luke said, I took a closer look at the ember-data source for the latest commit (Dec 11).

TLDR;要处理 ember-data 更新/创建错误,只需在 DS.Model 实例上定义 becameError()becameInvalid(errors).RESTadapter 的 AJAX 错误回调触发的级联最终会调用您定义的这些函数.

TLDR; to handle ember-data update/create errors, simply define becameError() and becameInvalid(errors) on your DS.Model instance. The cascade triggered by the RESTadapter's AJAX error callback will eventually call these functions you define.

示例:

App.Post = DS.Model.extend
  title: DS.attr "string"
  body: DS.attr "string"

  becameError: ->
    # handle error case here
    alert 'there was an error!'

  becameInvalid: (errors) ->
    # record was invalid
    alert "Record was invalid because: #{errors}"

这是完整的源代码:

在REST适配器中,给出了AJAX回调错误函数这里:

In the REST adapter, the AJAX callback error function is given here:

   this.ajax(this.buildURL(root, id), "PUT", {
      data: data,
      context: this,
      success: function(json) {
        Ember.run(this, function(){
          this.didUpdateRecord(store, type, record, json);
        });
      },
      error: function(xhr) {
        this.didError(store, type, record, xhr);
      }
    });

didError 定义为here> 然后它会根据响应依次调用商店的 recordWasInvalid 或 recordWasError:

didError is defined here and it in turn calls the store's recordWasInvalid or recordWasError depending on the response:

  didError: function(store, type, record, xhr) {
    if (xhr.status === 422) {
      var data = JSON.parse(xhr.responseText);
      store.recordWasInvalid(record, data['errors']);
    } else {
      store.recordWasError(record);
    }
  },

反过来,store.recordWasInvalidstore.recordWasError(定义了 此处 )调用记录(DS.Model)的处理程序.在无效的情况下,它将来自适配器的错误消息作为参数传递.

In turn, store.recordWasInvalid and store.recordWasError (defined here) call the record (a DS.Model)'s handlers. In the invalid case, it passes along error messages from the adapter as an argument.

 recordWasInvalid: function(record, errors) {
    record.adapterDidInvalidate(errors);
  },

  recordWasError: function(record) {
    record.adapterDidError();
  },

DS.Model.adapterDidInvalidateadapterDidError(定义为 此处) 只需 send('becameInvalid', errors)send('becameError') 最终引导我们到处理程序 此处:

DS.Model.adapterDidInvalidate and adapterDidError (defined here) simply send('becameInvalid', errors) or send('becameError') which finally leads us to the handlers here:

  didLoad: Ember.K,
  didUpdate: Ember.K,
  didCreate: Ember.K,
  didDelete: Ember.K,
  becameInvalid: Ember.K,
  becameError: Ember.K,

(Ember.K 只是一个返回 this 的虚拟函数.参见 这里)

(Ember.K is just a dummy function for returning this. See here)

所以,结论是,您只需在模型上为 becameInvalidbecameError 定义函数来处理这些情况.

So, the conclusion is, you simply need to define functions for becameInvalid and becameError on your model to handle these cases.

希望这对其他人有所帮助;文档现在肯定没有反映这一点.

Hope this helps someone else; the docs certainly don't reflect this right now.

这篇关于使用 Ember.js 数据 RESTAdapter 时应该如何处理错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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