Ember-data如何最好地处理非验证错误

时间:2013-07-15 19:34:48

标签: ember.js ember-data

处理来自ember-data的403错误的最佳方法是什么?

我想知道处理非验证错误的最佳方法,例如,如果不允许用户执行操作,我有一些代码可能会返回http 403。例如,我有以下代码:

contact.set 'something', 'something'

transaction = @get('store').transaction()

transaction.add(contact)

contact.one 'becameInvalid', (result) =>
  #display some error message

contact.one 'becameError', (result) =>
  # code does not get here for some reason
  transaction.rollback()
  #display some error message

transaction.commit()

如果发生403错误,则不会调用上述becameError处理程序,并且对象的状态机处于rootState.error状态,并且任何后续尝试在对象上设置属性都将失败,因为现在臭名昭着的,可怕的:

uncaught Error: Attempted to handle event `willSetProperty` on <Radium.Contact:ember2286:17> while in state rootState.error. Called with {reference: [object Object], store: <Radium.Store:ember3219>, name: name}

我的想法是我可以覆盖didError方法:

didError: function(store, type, record, xhr) {
  if (xhr.status === 422) {
    var json = JSON.parse(xhr.responseText),
        serializer = get(this, 'serializer'),
        errors = serializer.extractValidationErrors(type, json);

    store.recordWasInvalid(record, errors);
  } else {
    this._super.apply(this, arguments);
  }
},

我的问题是在状态机转换到rootState.error后如何让对象恢复到可用状态。

0 个答案:

没有答案