Class: FactoryFactory

FactoryFactory

new FactoryFactory(options)

A Factory factory, creates a Factory. Options passed in will be made available on the options property on the instance. In the case of using this module without browserify this function will be available on window.FactoryFactory.
Parameters:
Name Type Description
options Object Object containing the properties listed below.
Properties:
Name Type Argument Description
defaults function | Object <optional>
Object or function returning an object containing default for the options passed into the factory.
validate function | Object | String | Array.<(function()|Object|String|Array)> <optional>
Gets called after defaults are set, allows to validate options passed into the factory, can be a function, a string (representing a required property), a hashmap in the format of {'property': typeOrInstance}, {'name': 'string'} for example, or a mixed array of the previously mentioned.
initialize function <optional>
This function gets called with the context of the instance once it has been constructed.
props function | Object <optional>
May be provided as a function or object, the props for Object.create, not used if factory is provided.
factory function <optional>
If provided the instance will be created using this function, by default instances are created using Object.create.
prototype Object <optional>
The prototype of the 'class'.
Returns:
Factory
Example
const Factory = FactoryFactory({
  defaults(options) {
    return {
      defaultProperty: 'someValue'
    }
  },
  validate: [
    {
      // require name to be a string
      'name': 'string'
    },
    function (options) {
      if (options.name === 'bob') {
        throw new Error("name can't be bob");
      }
    },
    // make age required
    'age'
  ],
  initialize() {
    this.someMethod(); // call method on prototype
  },
  props(options) {
    // props passed to Object.create, this method is not called if factory is implemented
    return {
      someProp: {
        value: options.someOtherProp ? 'val1' : 'val2'
      }
    };
  },
  factory(options) {
    // create the instance manually
    return {
      options
    };
  },
  prototype: {
    someMethod() {
      alert(this.options.name); // alerts whatever name was passed into the factory
    }
  }
});

Members

<static> defaults :Object|function

Default options for all Factorys, works like the defaults property on a Factory
Type:
  • Object | function
Rik Hoffbauer 2015
Documentation generated by JSDoc 3.4.0 on 2015-12-20T23:05:06+01:00