Apex コントローラへのデータの受け渡し
Apex コントローラに渡すデータを設定するには、JavaScript で action.setParams() を使用します。
次の例では、firstName 属性値に基づいて、Apex コントローラの serverEcho メソッドで firstName 引数の値を設定します。
1var action = cmp.get("c.serverEcho");
2action.setParams({ firstName : "Jennifer") });要求ペイロードには、JSON として逐次化されるアクションデータが含まれます。
Apex コントローラのメソッドを次に示します。
1@AuraEnabled
2public static String serverEcho(String firstName) {
3 return ('Hello from the server, ' + firstName);
4}このフレームワークは、アクションデータを適切な Apex 型に並列化します。次の例では、firstName という文字列パラメータを使用しています。
異なるデータ型の例
Apex コントローラに異なる型のデータを送信するアプリケーションを見てみましょう。各ボタンは異なる型の一連のデータの受け渡しを開始します。
1<!-- actionParamTypes.app -->
2<aura:application controller="ApexParamTypesController">
3 <lightning:button label="putboolean" onclick="{!c.putbooleanc}"/>
4 <lightning:button label="putint" onclick="{!c.putintc}"/>
5 <lightning:button label="putlong" onclick="{!c.putlongc}"/>
6 <lightning:button label="putdecimal" onclick="{!c.putdecimalc}"/>
7 <lightning:button label="putdouble" onclick="{!c.putdoublec}"/>
8 <lightning:button label="putstring" onclick="{!c.putstringc}"/>
9 <lightning:button label="putobject" onclick="{!c.putobjectc}"/>
10 <lightning:button label="putblob" onclick="{!c.putblobc}"/>
11 <lightning:button label="putdate" onclick="{!c.putdatec}"/>
12 <lightning:button label="putdatetime" onclick="{!c.putdatetimec}"/>
13 <lightning:button label="puttime" onclick="{!c.puttimec}"/>
14 <lightning:button label="putlistoflistoflistofstring" onclick="{!c.putlistoflistoflistofstringc}"/>
15 <lightning:button label="putmapofstring" onclick="{!c.putmapofstringc}"/>
16 <lightning:button label="putcustomclass" onclick="{!c.putcustomclassc}"/>
17</aura:application>アプリケーションの JavaScript コントローラを次に示します。各アクションは、Apex コントローラに送信するアクションをキューに登録するヘルパーの putdatatype メソッドを呼び出します。このメソッドには 3 つのパラメータがあります。
- コンポーネント
- Apex メソッド名
- Apex メソッドに渡すデータ
1// actionParamTypesController.js
2({
3 putbooleanc : function(component, event, helper) {
4 helper.putdatatype(component, "c.pboolean", true);
5 },
6 putintc : function(component, event, helper) {
7 helper.putdatatype(component, "c.pint", 10);
8 },
9 putlongc : function(component, event, helper) {
10 helper.putdatatype(component, "c.plong", 2147483648);
11 },
12 putdecimalc : function(component, event, helper) {
13 helper.putdatatype(component, "c.pdecimal", 10.80);
14 },
15 putdoublec : function(component, event, helper) {
16 helper.putdatatype(component, "c.pdouble", 10.80);
17 },
18 putstringc : function(component, event, helper) {
19 helper.putdatatype(component, "c.pstring", "hello!");
20 },
21 putobjectc : function(component, event, helper) {
22 helper.putdatatype(component, "c.pobject", true);
23 },
24 putblobc : function(component, event, helper) {
25 helper.putdatatype(component, "c.pblob", "some blob as string");
26 },
27 // Date value is in ISO 8601 date format
28 putdatec : function(component, event, helper) {
29 helper.putdatatype(component, "c.pdate", "1997-01-31");
30 },
31 // Datetime value is in ISO 8601 datetime format
32 putdatetimec : function(component, event, helper) {
33 helper.putdatatype(component, "c.pdatetime", "1997-01-31T15:08:16.000Z");
34 },
35 // Set time in milliseconds.
36 // You can use (new Date()).getTime() to set the milliseconds
37 puttimec : function(component, event, helper) {
38 helper.putdatatype(component, "c.ptime", 3723004);
39 //helper.putdatatype(component, "c.ptime", (new Date()).getTime());
40 },
41 putlistoflistoflistofstringc : function(component, event, helper) {
42 helper.putdatatype(component, "c.plistoflistoflistofstring", [[['a','b'],['c','d']],[['e','f']]]);
43 },
44 putmapofstringc : function(component, event, helper) {
45 helper.putdatatype(component, "c.pmapofstring", {k1: 'v1'});
46 },
47 putcustomclassc : function(component, event, helper) {
48 helper.putdatatype(component, "c.pcustomclass", {
49 s: 'my string',
50 i: 10,
51 l: ['list value 1','list value 2'],
52 m: {k1: 'map value'},
53 os: {b: true}
54 });
55 },
56})ヘルパーには、Apex コントローラにデータを送信するユーティリティメソッドがあります。
1// actionParamTypesHelper.js
2({
3 putdatatype : function(component, actionName, val) {
4 var action = component.get(actionName);
5 action.setParams({ v : val });
6 action.setCallback(this, function(response) {
7 console.log(response.getReturnValue());
8 });
9 $A.enqueueAction(action);
10 }
11})次に、Apex コントローラを示します。
1public class ApexParamTypesController {
2 @AuraEnabled
3 public static Boolean pboolean(Boolean v){
4 System.debug(v);
5 return v;
6 }
7 @AuraEnabled
8 public static Integer pint(Integer v){
9 System.debug(v+v);
10 return v;
11 }
12 @AuraEnabled
13 public static Long plong(Long v){
14 System.debug(v);
15 return v;
16 }
17 @AuraEnabled
18 public static Decimal pdecimal(Decimal v){
19 System.debug(v);
20 return v;
21 }
22 @AuraEnabled
23 public static Double pdouble(Double v){
24 System.debug(v);
25 return v;
26 }
27 @AuraEnabled
28 public static String pstring(String v){
29 System.debug(v.capitalize());
30 return v;
31 }
32 @AuraEnabled
33 public static Object pobject(Object v){
34 System.debug(v);
35 return v;
36 }
37 @AuraEnabled
38 public static Blob pblob(Blob v){
39 System.debug(v.toString());
40 return v;
41 }
42 @AuraEnabled
43 public static Date pdate(Date v){
44 System.debug(v);
45 return v;
46 }
47 @AuraEnabled
48 public static DateTime pdatetime(DateTime v){
49 System.debug(v);
50 return v;
51 }
52 @AuraEnabled
53 public static Time ptime(Time v){
54 System.debug(v);
55 return v;
56 }
57 @AuraEnabled
58 public static List<List<List<String>>> plistoflistoflistofstring(List<List<List<String>>> v){
59 System.debug(v);
60 return v;
61 }
62 @AuraEnabled
63 public static Map<String, String> pmapofstring(Map<String, String> v){
64 System.debug(v);
65 return v;
66 }
67 @AuraEnabled
68 public static MyCustomApexClass pcustomclass(MyCustomApexClass v){
69 System.debug(v);
70 return v;
71 }
72}pcustomclass() Apex メソッドには、MyCustomApexClass というカスタム Apex 型のパラメータがあります。Apex クラスの各プロパティには @AuraEnabled アノテーションと、getter および setter が必要です。
1public class MyCustomApexClass {
2 @AuraEnabled
3 public String s {get; set;}
4 @AuraEnabled
5 public Integer i {get; set;}
6 @AuraEnabled
7 public List<String> l {get; set;}
8 @AuraEnabled
9 public Map <String, String> m {get; set;}
10 @AuraEnabled
11 public MyOtherCustomApexClass os {get; set;}
12}MyCustomApexClass Apex クラスには、別のカスタム Apex クラスの MyOtherCustomApexClass を型とするプロパティがあります。
1public class MyOtherCustomApexClass {
2 @AuraEnabled
3 public Boolean b {get; set;}
4}