# ES6の利用例

**URL:** <https://community.cybozu.dev/t/topic/593>\
**Category:** kintone 開発相談\
**Created:** [2017 年 10 月 29 日午後 3:25 UTC](https://community.cybozu.dev/t/topic/593 "2017-10-29T15:25:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account1740](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Legacy\_Account1740](https://community.cybozu.dev/u/Legacy_Account1740)\
**Post date:** [2017 年 10 月 29 日午後 3:25 UTC](https://community.cybozu.dev/t/topic/593/1 "2017-10-29T15:25:23Z")

</div>

今回はES6(ECMAScript 6th edition)を利用したコーディング例を紹介します。

ES6は2015年に標準化されており、現在の対応状況は[こちら](https://kangax.github.io/compat-table/es6/)のようになっております。  
ここでは、以前Tipsに投稿された[「一覧画面で条件に応じたレコード件数を取得するテクニック」](https://community.cybozu.dev/hc/ja/articles/202376464)のコードをES6に則って書き換えたいと思います。

## 利用したES6の特徴

- [クラス構文](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Classes)
- [テンプレート文字列](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/template_strings)
- [let](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/let)と[const](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/const)
- [アロー関数](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/arrow_functions)
- [Promiseオブジェクト](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Promise)(サンプルでは[kintone.api コールバック関数省略版](https://community.cybozu.dev/hc/ja/articles/204564604)を用いることで、コードをより簡潔にしています。)

## 作成したコード

```
(() => {
  "use strict";
  // 一覧ページ
  kintone.events.on('app.record.index.show', event => {
    // 現在の検索条件&処理中のレコード取得
    (new KintoneRecordManager).getProcessingRecords().then(response => {
      kintone.app.getHeaderMenuSpaceElement().innerHTML = `処理中のレコード件数： ${response.records.length}`;
    });
  });
})();
/**
 * kintone record manager
 */
class KintoneRecordManager {
  constructor(){
    this.records = [];
    this.appId = null;
    this.query = '';
    this.limit = 100;
    this.offset = 0;
  }
  // 現在の検索条件&処理中のレコード取得
  getProcessingRecords(){
    return new Promise(resolve => {
      this.appId = kintone.app.getId();
      this.query = `${kintone.app.getQueryCondition()} ${kintone.app.getQueryCondition() ? ' and ' : ''} ステータス = "処理中"`;
      this.getRecords().then(() => {
        resolve(this);
      });
    });
  }
  // レコード取得
  getRecords(){
    return kintone.api('/k/v1/records', 'GET', {
      app: this.appId,
      query: `${this.query} limit ${this.limit} offset ${this.offset}`
    }).then(response => {
      let len;
      Array.prototype.push.apply(this.records, response.records);
      len = response.records.length;
      this.offset += len;
      if (len < this.limit) {
        this.ready = true;
      } else {
        return this.getRecords();
      }
    });
  }
}

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1517](https://avatars.discourse-cdn.com/v4/letter/l/4bbf92/32.png) [@Legacy\_Account1517](https://community.cybozu.dev/u/Legacy_Account1517)\
**Post date:** [2017 年 10 月 30 日午前 8:09 UTC](https://community.cybozu.dev/t/topic/593/2 "2017-10-30T08:09:35Z")

</div>

江田さん

ご紹介ありがとうございます。大変参考になります。

はやく IE が動作対象から外れて、ES6 を使えるようにしてほしいですね。

改善点としては、件数取得に全件読み込む必要が無く、totalCount パラメータ指定で良いと思います。

もとの「一覧画面で条件に応じたレコード件数を取得するテクニック」が投稿された時点では、まだtotalCount パラメータが無かったためだと思います。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1740](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Legacy\_Account1740](https://community.cybozu.dev/u/Legacy_Account1740)\
**Post date:** [2017 年 11 月 2 日午前 12:05 UTC](https://community.cybozu.dev/t/topic/593/3 "2017-11-02T00:05:47Z")

</div>

rex0220様

お世話になっております。  
なるほど！そのような指定ができるのですね！  
勉強不足でした。  
大変参考になります！ありがとうございます！
