レコードの保存
Lightning データサービスを使用してレコードを保存するには、force:recordData コンポーネントの saveRecord をコールして、保存操作の完了後に呼び出されるコールバック関数を渡します。
Lightning データサービスの保存操作は、次の 2 つの状況で行われます。
- 変更を既存のレコードに保存する場合
- 新しいレコードを作成して保存する場合
変更を既存のレコードに保存するには、レコードを EDIT モードで読み込み、force:recordData コンポーネントの saveRecord をコールします。
新しいレコードを保存するには作成する必要があるため、「レコードの作成」に記載のとおり、レコードテンプレートからレコードを作成します。次に、force:recordData コンポーネントの saveRecord をコールします。
EDIT モードのレコードの読み込み
更新される可能性のあるレコードを読み込むには、force:recordData タグの mode 属性を「EDIT」に設定します。mode を明示的に設定すること以外は、編集するレコードの読み込みも他の目的の読み込みと同じです。
saveRecord のコールによるレコードの変更の保存
保存操作を実行するには、適切なコントローラアクションハンドラから、force:recordData コンポーネントの saveRecord をコールします。saveRecord は、操作の完了時に呼び出されるコールバック関数という 1 つの引数を取ります。このコールバック関数は、その唯一のパラメータとして SaveRecordResult オブジェクトを受け取ります。SaveRecordResult には、操作の成否や、結果の処理に使用できるその他の詳細を示す state 属性が含まれます。
レコードの保存
次の例は、Lightning データサービスを使用したレコードの保存の要点を示しています。この例は、レコードページで使用されることを意図しています。レコード ID は、force:hasRecordId インターフェースによって追加される暗黙的な recordId 属性によって指定されます。
ldsSave.cmp
このコンポーネントは、EDIT モードに設定された force:recordData を使用してレコードを読み込み、レコードの値を編集するためのフォームを提供します(この単純な例では、レコード名項目のみです)。
1<<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId">
2
3 <aura:attribute name="record" type="Object"/>
4 <aura:attribute name="simpleRecord" type="Object"/>
5 <aura:attribute name="recordError" type="String"/>
6
7 <force:recordData aura:id="recordHandler"
8 recordId="{!v.recordId}"
9 layoutType="FULL"
10 targetRecord="{!v.record}"
11 targetFields="{!v.simpleRecord}"
12 targetError="{!v.recordError}"
13 mode="EDIT"
14 recordUpdated="{!c.handleRecordUpdated}"
15 />
16
17 <!-- Display a header with details about the record -->
18 <div class="slds-page-header" role="banner">
19 <p class="slds-text-heading--label">Edit Record</p>
20 <h1 class="slds-page-header__title slds-m-right--small
21 slds-truncate slds-align-left">{!v.simpleRecord.Name}</h1>
22 </div>
23
24 <!-- Display Lightning Data Service errors, if any -->
25 <aura:if isTrue="{!not(empty(v.recordError))}">
26 <div class="recordError">
27 <ui:message title="Error" severity="error" closable="true">
28 {!v.recordError}
29 </ui:message>
30 </div>
31 </aura:if>
32
33 <!-- Display an editing form -->
34 <lightning:input aura:id="recordName" name="recordName" label="Name"
35 value="{!v.simpleRecord.Name}" required="true"/>
36
37 <lightning:button label="Save Record" onclick="{!c.handleSaveRecord}"
38 variant="brand" class="slds-m-top--medium"/>
39</aura:component>
ldsSaveController.js
この例の handleSaveRecord アクションは最小限のものです。フォームの検証や実際のエラー処理はありません。フォームに入力された内容はすべて、レコードへの保存の対象になります。
1({
2 handleSaveRecord: function(component, event, helper) {
3 component.find("recordHandler").saveRecord($A.getCallback(function(saveResult) {
4 // NOTE: If you want a specific behavior(an action or UI behavior) when this action is successful
5 // then handle that in a callback (generic logic when record is changed should be handled in recordUpdated event handler)
6 if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
7 // handle component related logic in event handler
8 } else if (saveResult.state === "INCOMPLETE") {
9 console.log("User is offline, device doesn't support drafts.");
10 } else if (saveResult.state === "ERROR") {
11 console.log('Problem saving record, error: ' + JSON.stringify(saveResult.error));
12 } else {
13 console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
14 }
15 }));
16 },
17
18 /**
19 * Control the component behavior here when record is changed (via any component)
20 */
21 handleRecordUpdated: function(component, event, helper) {
22 var eventParams = event.getParams();
23 if(eventParams.changeType === "CHANGED") {
24 // get the fields that changed for this record
25 var changedFields = eventParams.changedFields;
26 console.log('Fields that are changed: ' + JSON.stringify(changedFields));
27 // record is changed, so refresh the component (or other component logic)
28 var resultsToast = $A.get("e.force:showToast");
29 resultsToast.setParams({
30 "title": "Saved",
31 "message": "The record was updated."
32 });
33 resultsToast.fire();
34
35 } else if(eventParams.changeType === "LOADED") {
36 // record is loaded in the cache
37 } else if(eventParams.changeType === "REMOVED") {
38 // record is deleted and removed from the cache
39 } else if(eventParams.changeType === "ERROR") {
40 // there’s an error while loading, saving or deleting the record
41 }
42 }
43})