技術メモ(主に自分向け)

短期記憶の自分向けの技術メモです。

コンポーネントにApexの処理を使用する方法

コンポーネントでApex処理を呼ぶ。

(例)
Apex側

public class SampleApexClass {
 public static String getSample() {…}
}

cmp側

<aura:component controller="SampleApexClass" implements="forceCommunity:availableForAllPageTypes">
 <a href="javascript:void(0);" onclick="{!c.sampleJs}">クリック</a>
</aura:component>


js側

sampleJs : function (component, event, helper) {
 var action = component.get("c.getSample");
 action.setCallback(this, function(response){
  var rtnValue = response.getReturnValue(); //戻り値取得
  …
 });
 $A.enqueueAction(action);
}


引数を持っているApexメソッドの場合
(例)
Apex側

public class SampleApexClass2 {
 public static String getSample2(String str1, String str2) {…}
}


js側(cmpは上記と同様の書き方のため省略)

sampleJs : function (component, event, helper) {
 var test1 = ''あああ;
 var test2 = 'いいい';
 var action = component.get("c.getSample");
 action.setParams({str1:test1, str2:test2}); // Apex側の引数名と同名しないと渡せない
 action.setCallback(this, function(response){
  var rtnValue = response.getReturnValue(); //戻り値取得
  …
 });
 $A.enqueueAction(action);
}