title: Converting Singleton JS objects to use ES6 classes
body: <p>I'm using ES6 with the Webpack es6-transpiler per my article here: <a href="http://www.railsonmaui.com/blog/2014/10/02/integrating-webpack-and-the-es6-transpiler-into-an-existing-rails-project/">http://www.railsonmaui.com/blog/2014/10/02/integrating-webpack-and-the-es6-transpiler-into-an-existing-rails-project/</a></p>
  
  <p>Does it make any sense to convert two Singleton objects to use ES6 Classes?</p>
  
  <pre><code>import { CHANGE_EVENT } from "../constants/Constants";
  
  var EventEmitter = require('events').EventEmitter;
  var merge = require('react/lib/merge');
  
  var _flash = null;
  
  var BaseStore = merge(EventEmitter.prototype, {
  
    emitChange: function() {
      this.emit(CHANGE_EVENT);
    },
  
    /**
     * @param {function} callback
     */
    addChangeListener: function(callback) {
      this.on(CHANGE_EVENT, callback);
    },
  
    /**
     * @param {function} callback
     */
    removeChangeListener: function(callback) {
      this.removeListener(CHANGE_EVENT, callback);
    },
  
    getFlash: function() {
      return _flash;
    },
  
    setFlash: function(flash) {
      _flash = flash;
    }
  });
  
  export { BaseStore };
  </code></pre>
  
  <p>This is file ManagerProducts.jsx that has a singleton that should extend from BaseStore.</p>
  
  <pre><code>/**
   * Client side store of the manager_product resource
   */
  import { BaseStore } from "./BaseStore";
  import { AppDispatcher } from '../dispatcher/AppDispatcher';
  import { ActionTypes } from '../constants/Constants';
  import { WebAPIUtils } from '../utils/WebAPIUtils';
  import { Util } from "../utils/Util";
  var merge = require('react/lib/merge');
  
  var _managerProducts = [];
  
  var receiveAllDataError = function(action) {
    console.log("receiveAllDataError %j", action);
    WebAPIUtils.logAjaxError(action.xhr, action.status, action.err);
  };
  
  var ManagerProductStore = merge(BaseStore, {
    getAll: function() {
      return _managerProducts;
    }
  });
  
  var receiveAllDataSuccess = function(action) {
    _managerProducts = action.data.managerProducts;
    //ManagerProductStore.setFlash({ message: "Manager Product data loaded"});
  };
  
  
  ManagerProductStore.dispatchToken = AppDispatcher.register(function(payload) {
    var action = payload.action;
    if (Util.blank(action.type)) { throw `Invalid action, payload ${JSON.stringify(payload)}`; }
  
    switch(action.type) {
      case ActionTypes.RECEIVE_ALL_DATA_SUCCESS:
        receiveAllDataSuccess(action);
        break;
      case ActionTypes.RECEIVE_ALL_DATA_ERROR:
        receiveAllDataError(action);
        break;
      default:
        return true;
    }
    ManagerProductStore.emitChange();
    return true;
  });
  
  export { ManagerProductStore };
  </code></pre>