はじめに
(Mac環境の記事ですが、Windows環境も同じ手順になります。環境依存の部分は読み替えてお試しください。)
この記事を最後まで読むと、次のことができるようになります。
- データの引き渡しなど複雑な処理の実装方法について理解する
- JavaScriptを使って実装する
イメージ画像
Input | Output | |
---|---|---|
>>> |
countUp: function(btn, context) {
var rowIndex = context.rowIndex;
var sectionIndex = context.sectionIndex;
var rowData = context.widgetInfo.data[rowIndex];
if (Number(rowData.txtCounter.text) + 1 > 20) {
return;
}
rowData.txtCounter.text = Number(rowData.txtCounter.text) + 1;
context.widgetInfo.setDataAt(rowData, rowIndex, sectionIndex);
}
関連する記事
- Kony AppPlatformを使ってiOS/Androidアプリを作成する
- Kony AppPlatformで作成したiOS/AndroidアプリのAuto Layoutについて学ぶ
- Kony AppPlatformで作成したiOS/AndroidアプリとSalesforceをデータ連携する
実行環境
環境 | Ver. |
---|---|
macOS Mojave | 10.14.1 |
Kony Visualizer | 8.3.10 |
ソースコード
実際に実装内容やソースコードを追いながら読むとより理解が深まるかと思います。是非ご活用ください。
Kony AppPlatformの特徴
簡単な処理はプログラミングレスで開発できますが、複雑な処理はコーディングによる実装が必要となります。クロスプラットフォームを採用しているため、iOSアプリ(Swift)やAndroidアプリ(Java)のプログラミング言語を使用せずJavaScript
で実装します。また、Swiftの内部構成と同様に1つの画面(Form)に対して1つのControllerが自動で割り当てられます。
コーディングの方法
画面(Form)を作成するとController
とControllerActions
が自動生成されます。コードを書く際はこの2つのControllerに実装します。
自動生成 | 説明 |
---|---|
Controller | 汎用的な処理はControllerにコーディングします。作成したFunctionはActionのInvoke Functionから呼び出して使用します。 |
ControllerActions | ActionのAdd Snippetなどでコーディングしたものが自動的に追加されます。ControllerActionsに直接コーディングする事はできません。 |
実装サンプル
- ボタンを押してテキストボックスの値をカウントアップする
項目名 | オブジェクト | 説明 |
---|---|---|
lblTitle | ラベル | タイトル |
txtCounter | テキストボックス | カウント表示 |
btnCountUp | ボタン | カウントアップ |
画面イメージ
Controllerに実装するケース
Controller画面を作成します。Form名は
frmController
とします。-
Projectタブ -> Mobile -> Controllers ->
frmControllerController
をクリックしてdefine
の中にカウントアップのコードを書きます。frmControllerController.countUp.jscountUp: function() { this.view.txtCounter.text = Number(this.view.txtCounter.text) + 1; }
-
Projectタブ -> Mobile -> Forms -> frmController ->
btnCountUp
をクリックします。次にPropertiesタブ -> ActionたタブのonClick
のEdit
をクリックします。 -
Functionsの中から
Invoke Function
をクリックします。次にFunction NameからcountUp
を設定します。
ControllerActionsに実装するケース
ControllerActions画面を作成します。Form名は
frmControllerActions
とします。-
Projectタブ -> Mobile -> Forms -> frmControllerActions ->
btnCountUp
をクリックします。次にPropertiesタブ -> ActionたタブのonClick
のEdit
をクリックします。 -
Functionsの中から
Add Snippet
をクリックします。次にカウントアップのコードを書きます。frmControllerActionsControllerActions.jsself.view.txtCounter.text = Number(this.view.txtCounter.text) + 1;
-
Projectタブ -> Mobile -> Controllers ->
frmControllerActionsControllerActions
をクリックしてdefine
の中にSnippetのコードが自動で追加されたことを確認します。
複雑な処理の実装
シナリオ
- アプリは入力画面と出力画面の2つで構成する
- 入力画面はリスト一覧を持ち1件ごとに個数のカウントアップ/ダウンを可能とする
- 出力画面は入力データを引き継ぎデータを結合して表示する
画面イメージ
Input | Output | |
---|---|---|
>>> |
事前準備
各画面を作成してコーディングの準備をします。
-
入力画面
-
リストセクション
項目名 オブジェクト 説明 segSection Template テンプレート flxSection FlexContainer グループ lblName Label タイトル txtCounter TextBox カウンター btnCountDown Button カウントダウン btnCountUp Button カウントアップ -
入力画面
項目名 オブジェクト 説明 frmInput Form 入力画面 flxInput FlexContainer グループ lblTitle Label タイトル segList Segment リスト btnOutput Button 出力ボタン
-
-
出力画面
項目名 オブジェクト 説明 frmOutput Form 出力画面 flxOutput FlexContainer グループ lblTitle Label タイトル lblResult Label 結果 btnBack Button 戻るボタン
リストデータ初期化
-
Projectタブ -> Mobile -> Controllers ->
frmInputController
をクリックしてdefine
の中にデータを初期化するコードを書きます。frmInputController.initData.jsinitData: function() { this.view.segList.setData([ { lblName: { text: "Data1" }, txtCounter: { text: 0 }, btnCountDown: { onClick: this.countDown.bind(this) }, btnCountUp: { onClick: this.countUp.bind(this) } }, { lblName: { text: "Data2" }, txtCounter: { text: 0 }, btnCountDown: { onClick: this.countDown.bind(this) }, btnCountUp: { onClick: this.countUp.bind(this) } }, { lblName: { text: "Data3" }, txtCounter: { text: 0 }, btnCountDown: { onClick: this.countDown.bind(this) }, btnCountUp: { onClick: this.countUp.bind(this) } }, { lblName: { text: "Data4" }, txtCounter: { text: 0 }, btnCountDown: { onClick: this.countDown.bind(this) }, btnCountUp: { onClick: this.countUp.bind(this) } }, { lblName: { text: "Data5" }, txtCounter: { text: 0 }, btnCountDown: { onClick: this.countDown.bind(this) }, btnCountUp: { onClick: this.countUp.bind(this) } } ]); }, countDown: function(btn, context) { alert("rowIndex=" + context.rowIndex); }, countUp: function(btn, context) { alert("rowIndex=" + context.rowIndex); }
-
Projectタブ -> Mobile -> Forms ->
frmInput
をクリックします。次にPropertiesタブ -> Actionたタブのinit
のEdit
をクリックします。 -
Functionsの中から
Invoke Function
をクリックします。次にFunction NameからinitData
を設定します。
カウント処理
-
Projectタブ -> Mobile -> Controllers ->
frmInputController
をクリックしてdefine
の中にカウントダウンのコードを書きます。frmInputController.countDown.jscountDown: function(btn, context) { var rowIndex = context.rowIndex; var sectionIndex = context.sectionIndex; var rowData = context.widgetInfo.data[rowIndex]; if (Number(rowData.txtCounter.text) - 1 < 0) { return; } rowData.txtCounter.text = Number(rowData.txtCounter.text) - 1; context.widgetInfo.setDataAt(rowData, rowIndex, sectionIndex); }
-
同様に
define
の中にカウントアップのコードを書きます。frmInputController.countUp.jscountUp: function(btn, context) { var rowIndex = context.rowIndex; var sectionIndex = context.sectionIndex; var rowData = context.widgetInfo.data[rowIndex]; if (Number(rowData.txtCounter.text) + 1 > 20) { return; } rowData.txtCounter.text = Number(rowData.txtCounter.text) + 1; context.widgetInfo.setDataAt(rowData, rowIndex, sectionIndex); }
出力処理
-
Projectタブ -> Mobile -> Controllers ->
frmInputController
をクリックしてdefine
の中に出力処理のコードを書きます。frmOutputをインスタンス化して、navigate
で画面遷移とリストデータ(JSON)の引き渡しを実装します。frmInputController.viewOutput.jsviewOutput: function() { var frmOutput = new kony.mvc.Navigation("frmOutput"); frmOutput.navigate({data: this.view.segList.data}); }
-
Projectタブ -> Mobile -> Controllers ->
frmOutputController
をクリックしてdefine
の中に出力処理のコードを書きます。onNavigate
で画面遷移のイベントを受け取り、引き渡されたデータ(JSON)を加工して出力します。frmOutputController.onNavigate.jsonNavigate: function(params) { this.view.lblResult.text = "none"; for (var data of params.data) { if (Number(data.txtCounter.text) === 0) { continue; } if (this.view.lblResult.text == "none") { this.view.lblResult.text = data.lblName.text; } else { this.view.lblResult.text += "\n" + data.lblName.text; } this.view.lblResult.text += "\t : " + data.txtCounter.text; } }
-
Projectタブ -> Mobile -> Forms -> frmInput ->
btnOutput
をクリックします。次にPropertiesタブ -> ActionたタブのonClick
のEdit
をクリックします。 -
Functionsの中から
Invoke Function
をクリックします。次にFunction NameからviewOutput
を設定します。 -
Projectタブ -> Mobile -> Forms -> frmOutput ->
btnBack
をクリックします。次にPropertiesタブ -> ActionたタブのonClick
のEdit
をクリックします。 -
Navigationの中から
Navigate to Form
をクリックします。次にfrmInput
を設定します。