Boost!for kintone
HOW TO WRITE

イベント処理の記述方法

イベントハンドラーをイベントに登録する方法と、削除する方法を紹介します。

01

イベントハンドラーを登録する

構文
bst.event.on(type, handler)
パラメータ
パラメータ名 指定する値 説明
type 文字列、または文字列の配列

イベントハンドラーをバインドする対象のイベントタイプ、またはイベントタイプの配列を指定します。

handler 関数

イベント発生時に実行されるハンドラーです。

イベントのハンドラー内でPromiseオブジェクトをreturnすると、非同期処理の完了を待って次の処理を開始します。

戻り値
ありません。
サンプル
(() => {
	"use strict";
	const handler = (event) => {
		console.log(event);
	};
	bst.event.on('bst.view.load', handler);
})();
02

イベントハンドラーを削除する

構文
bst.event.off(type, handler)
パラメータ
パラメータ名 指定する値 説明
type 文字列、または文字列の配列

削除対象のイベントハンドラーをバインドしたイベントタイプ、またはイベントタイプの配列を指定します。

handler 関数

削除対象のイベントハンドラーです。バインド時と同一のイベントハンドラーを指定します。

戻り値
ありません。
サンプル
(() => {
	"use strict";
	const handler = (event) => {
		console.log(event);
	};
	bst.event.on('bst.view.load', handler);

	bst.event.off('bst.view.load', handler);
})();
EVENT OBJECT

eventオブジェクトで実行できる操作

イベントハンドラーで、eventオブジェクトを使って実行できる操作を紹介します。

01

フィールドの値を書き換える

イベントハンドラーでレコードオブジェクトのフィールドの値を書き換えてeventオブジェクトを返すと、レコードのフィールドの値が更新されます。

サンプル
(() => {
	"use strict";
	bst.event.on('bst.change.field_1_', (event) => {
		const record = event.record;
		if (record.field_1_.value=='')
		{
			record.field_2_.value='option1';
		}
		return event;
	});
})();
02

ルックアップを自動取得する

イベントハンドラーでルックアップフィールドのlookupプロパティにtrueを入力してeventオブジェクトを返すと、そのルックアップフィールドの値を自動で取得します。

ルックアップフィールドをクリアする場合は、valueプロパティを空にした状態で、lookupプロパティにtrueを入力します。

サンプル
(() => {
	"use strict";
	bst.event.on('bst.edit.load', (event) => {
		const record = event.record;
		if (!record.field_3_.value)
		{
			record.field_3_.value=1;
			record.field_3_.lookup=true;
		}
		return event;
	});
})();
03

後続処理を中止させる

イベントハンドラーでerrorプロパティにtrueを入力してeventオブジェクトを返すと、後続の処理を含む操作がキャンセルされます。

サンプル
(() => {
	"use strict";
	bst.event.on('bst.create.submit', (event) => {
		const record = event.record;
		if (record.field_1_.value=='')
		{
			bst.alert('No value entered.');
			event.error=true;
		}
		return event;
	});
})();