1

REST アダプターで Ember データを使用しています。this.transaction.commit() を使用してレコードを保存し、サーバーが 422 検証エラーで応答した場合、このケースは「becameError」イベントを使用してキャプチャできます。

ただし、フォーム フィールドのデータを変更し、もう一度 [保存] をクリックすると (つまり、2 回目の this.transaction.commit() を実行しても)、何も起こりません。無効な状態であるため、トランザクションはコミットされません ...

どうすればこれを解決できますか?

4

3 に答える 3

1

モデルの stateManager を介して、モデルをコミットされていない状態に戻すことができます。既存のレコードの場合は、次のように移行しloaded.updated.committedます。

model.get('stateManager').transitionTo('loaded.updated.uncommitted')

新しいレコードの場合は、への移行loaded.created.uncommitted

model.get('stateManager').transitionTo('loaded.created.uncommitted')

ember-data API にもっと良い方法があるまで、これを回避策と考えてください。

エラー状態のときに Ember Data Models で何ができるかを参照してください。詳細については、https://gist.github.com/intrica/4773420

于 2013-08-08T22:51:50.587 に答える
0

恐ろしい回避策として、代わりにレコードのクローンを保存してみることができます。これにより、元のレコードがそのまま残ります。

保存が成功したら、元のレコードを削除します。そうでない場合は、クローンを削除して、新しいクローンで再試行してください。

于 2015-06-18T06:05:13.513 に答える
0

becomeInvalid 状態の場合、('loaded.created.uncommitted') 状態に遷移した後、ストアの defaultTransactionを使用して再コミットする必要があります。

以下のコードを参照してください - transaction.commit() または defaultTransaction.commit() を使用するかどうかを知るための非常に汚いチェック

save: function () {

//Local commit - author record goes in Flight state
this.transaction.commit();

//After a becameInvalid state, transaction.commit() does not work; use defaultTransaction in that case
//Is this the only way to do this ?
if (this.get('stateManager.currentState.name') == "uncommitted") {
  this.get('store').get("defaultTransaction").commit();
}

var author = this.get('model');

author.one('didCreate', this, function () {
  this.transitionToRoute('author.edit', author);
});

//If response is error (e.g. REST API not accessible): becameError fires
author.one('becameError', this, function () {
  this.get('stateManager').transitionTo('loaded.created.uncommitted');
});

//If response is 422 (validation problem at server side): becameInvalid fires
author.one('becameInvalid', this, function () {
  this.set('errors', this.get('content.errors'));

  //Does set stateManager.currentState.name to uncommitted, but when committing again, nothing happens.  
  this.get('stateManager').transitionTo('loaded.created.uncommitted')
});

}、

于 2013-08-09T09:35:05.080 に答える