select2 ajax カスタマイズ(kintone js api使用)

古い書き方
select boxを正しく選択できない不具合あり

function appendSelectContent(event){
  var $select = newSelectContent();
  var $div = $('<div>');
  $div.append($select);
  kintone.app.record.getSpaceElement('selectField').append($div[0])
  $select.select2({
    placeholder: '選択してください',
    width: '200px',
    minimumInputLength: 1,
    query: function (query) {
      var queryStr = "name like \"" + query.term + "\""
      getRecords(99, queryStr, function(resp){
        var data = {results: []};
        $.each(resp.records, function(i,v){
          data.results.push({id: i+1, text: v.name.value})
        });
        query.callback(data);
      })
    }
  });
  return event
}

新しい書き方

function appendSelectContent(event){
  var $select = newSelectContent();
  var $div = $('<div>');
  $div.append($select);
  kintone.app.record.getSpaceElement('selectField').append($div[0])

  $.fn.select2.amd.define('select2/data/customAdapter',[
          'select2/data/array',
          'select2/utils'
      ],
      function (ArrayAdapter, Utils) {
          function CustomDataAdapter ($element, options) {
              CustomDataAdapter.__super__.constructor.call(this, $element, options);
          }
          Utils.Extend(CustomDataAdapter, ArrayAdapter);
          CustomDataAdapter.prototype.query = function (params, callback) {
            var data = {results: []};
            var query = "name like \"" + params.term + "\""
            getRecords(99, query, function(resp){
              $.each(resp.records, function(i,v){
                data.results.push({id: i+1, text: v.name.value})
              });
              callback(data);
            })
          };
          return CustomDataAdapter;
      }
  );
  var customAdapter=$.fn.select2.amd.require('select2/data/customAdapter');
  $select.select2({
    placeholder: '選択してください',
    width: '200px',
    minimumInputLength: 1,
    dataAdapter: customAdapter
  });
  return event
}

参考

http://select2.github.io/select2/