{"size": 918, "ext": "js", "max_stars_repo_path": "src/ractive-decorators-selectize.js", "max_stars_repo_name": "mymth/ractive-decorators-selectize", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/ractive-decorators-selectize.js", "max_issues_repo_name": "mymth/ractive-decorators-selectize", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/ractive-decorators-selectize.js", "max_forks_repo_name": "mymth/ractive-decorators-selectize", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 19.5319148936, "max_line_length": 80, "alphanum_fraction": 0.6023965142}
import $ from 'jquery';

const selectizeDecorator = function selectizeDecorator(node, type = 'default') {
  const keypath = this.getContext(node).getBindingPath();
  const types = selectizeDecorator.types;
  const options = types.hasOwnProperty(type) ? types[type] : types.default;
  let obsHandle;
  let setting = false;

  $(node).selectize(options).on('change', () => {
    if (setting) {
      return;
    }

    setting = true;
    this.updateModel();
    setting = false;
  });

  if (keypath) {
    obsHandle = this.observe(keypath, (newValue) => {
      if (setting) {
        return;
      }

      setting = true;
      node.selectize.setValue(newValue);
      setting = false;
    });
  }

  return {
    teardown() {
      node.selectize.destroy();
      if (obsHandle) {
        obsHandle.cancel();
      }
    }
  };
};

selectizeDecorator.types = {
  default: {},
};

export default selectizeDecorator;
