レコードの変更
レコードの変更時に force:recordData を使用してさらに詳細なタスクを実行するには、recordUpdated イベントを処理します。レコードの読み込み、更新、削除による変更を処理して、変更種別ごとにアクションを適用できます。
コンポーネントがレコードデータに固有のロジックを実行する場合は、レコードの変更時にそのロジックをもう一度実行する必要があります。よくある例は、レコードの値に応じてレコードに適用されるアクションが変化するビジネスプロセスです。たとえば、営業サイクルのフェーズごとに商談に適用されるアクションが異なります。
例
コンポーネントで recordUpdated イベントを処理することを宣言します。
1<force:recordData aura:id="forceRecord"
2 recordId="{!v.recordId}"
3 layoutType="FULL"
4 targetRecord="{!v._record}"
5 targetFields="{!v.simpleRecord}"
6 targetError="{!v._error}"
7 recordUpdated="{!c.recordUpdated}" />変更を処理するアクションハンドラを実装します。
1({
2 recordUpdated: function(component, event, helper) {
3
4 var changeType = event.getParams().changeType;
5
6 if (changeType === "ERROR") { /* handle error; do this first! */ }
7 else if (changeType === "LOADED") { /* handle record load */ }
8 else if (changeType === "REMOVED") { /* handle record removal */ }
9 else if (changeType === "CHANGED") { /* handle record change */ }
10})編集モードでレコードを読み込む場合は、現在進行中の編集が上書きされないように、レコードが自動的に更新されません。レコードを更新するには、アクションハンドラで reloadRecord メソッドを使用します。
1<force:recordData aura:id="forceRecord"
2 recordId="{!v.recordId}"
3 layoutType="FULL"
4 targetRecord="{!v._record}"
5 targetFields="{!v.simpleRecord}"
6 targetError="{!v._error}"
7 mode="EDIT"
8 recordUpdated="{!c.recordUpdated}" />1({
2 recordUpdated : function(component, event, helper) {
3
4 var changeType = event.getParams().changeType;
5
6 if (changeType === "ERROR") { /* handle error; do this first! */ }
7 else if (changeType === "LOADED") { /* handle record load */ }
8 else if (changeType === "REMOVED") { /* handle record removal */ }
9 else if (changeType === "CHANGED") {
10 /* handle record change; reloadRecord will cause you to lose your current record, including any changes you’ve made */
11 component.find("forceRecord").reloadRecord();}
12 }
13})