Members
-
<readonly> registered :Object.<string, Object>
An object containing all registered classes.
-
Examples
{ "<name>": { "name": <string>, "klass": <function>, "priority": <number> }, "<name>": { "name": <string>, "klass": <function>, "priority": <number> }, ... }
Details
Methods
-
contains( name ) → {boolean}
Checks if the factory contains a class registered using the supplied
name
. -
Parameters
Name Type Description name
string The name of the class to check.
Returns
Examples
// create a new instance of the factory, this is usually exposed by the class that will be using the factory. var factory = new FooGallery.utils.Factory(); // create a class to register function Test(){} // register the class with the factory with the default priority factory.register( "test", Test ); // test if the class was registered console.log( factory.contains( "test" ) ); // => true
Details
-
load( overrides, arg1 [, ...argN ] ) → {Array.<Object>}
Creates new instances of all registered classes using there registered priority and the supplied arguments.
-
Description
The class indexes within the result array are determined by the
priority
they were registered with, the higher thepriority
the lower the index.This method is designed to be used when all registered classes share a common interface or base type and constructor arguments.
Parameters
Name Type Attributes Description overrides
Object.<string, function()> An object containing classes to override any matching registered classes with, if no overrides are required you can pass
false
ornull
.arg1
* The first argument to supply when creating new instances of all registered classes.
argN
* <optional>
<repeatable>Any number of additional arguments to supply when creating new instances of all registered classes.
Returns
Examples
// create a new instance of the factory, this is usually exposed by the class that will be using the factory. var factory = new FooGallery.utils.Factory(); // create a base Extension class var Extension = FooGallery.utils.Class.extend({ construct: function( type, options ){ this.type = type; this.options = options; }, getType: function(){ return this.type; } }); // create various item, this would usually be in another file var MyExtension1 = Extension.extend({ construct: function(options){ this._super( "my-extension-1", options ); } }); factory.register( "my-extension-1", MyExtension1, 0 ); // create various item, this would usually be in another file var MyExtension2 = Extension.extend({ construct: function(options){ this._super( "my-extension-2", options ); } }); factory.register( "my-extension-2", MyExtension2, 1 ); // load all registered classes according to there priority passing the options to all constructors var loaded = factory.load( null, {"something": true} ); // only two classes should be loaded console.log( loaded.length ); // => 2 // the MyExtension2 class is loaded first due to it's priority being higher than the MyExtension1 class. console.log( loaded[0] instanceof MyExtension2 && loaded[0] instanceof Extension ); // => true console.log( loaded[1] instanceof MyExtension1 && loaded[1] instanceof Extension ); // => true // do something with the loaded classes
// create a new instance of the factory, this is usually exposed by the class that will be using the factory. var factory = new FooGallery.utils.Factory(); // create a base Extension class var Extension = FooGallery.utils.Class.extend({ construct: function( type, options ){ this.type = type; this.options = options; }, getType: function(){ return this.type; } }); // create a new extension, this would usually be in another file var MyExtension1 = Extension.extend({ construct: function(options){ this._super( "my-extension-1", options ); } }); factory.register( "my-extension-1", MyExtension1, 0 ); // create a new extension, this would usually be in another file var MyExtension2 = Extension.extend({ construct: function(options){ this._super( "my-extension-2", options ); } }); factory.register( "my-extension-2", MyExtension2, 1 ); // create a custom extension that is not registered but overrides the default "my-extension-1" var UpdatedMyExtension1 = MyExtension1.extend({ construct: function(options){ this._super( options ); // do something different to the original MyExtension1 class } }); // load all registered classes but swaps out the registered "my-extension-1" for the supplied override. var loaded = factory.load( {"my-extension-1": UpdatedMyExtension1}, {"something": true} ); // only two classes should be loaded console.log( loaded.length ); // => 2 // the MyExtension2 class is loaded first due to it's priority being higher than the UpdatedMyExtension1 class which inherited a priority of 0. console.log( loaded[0] instanceof MyExtension2 && loaded[0] instanceof Extension ); // => true console.log( loaded[1] instanceof UpdatedMyExtension1 && loaded[1] instanceof MyExtension1 && loaded[1] instanceof Extension ); // => true // do something with the loaded classes
Details
-
make( name, arg1 [, ...argN ] ) → {Object}
Create a new instance of a class registered with the supplied
name
and arguments. -
Parameters
Name Type Attributes Description name
string The name of the class to create.
arg1
* The first argument to supply to the new instance.
argN
* <optional>
<repeatable>Any number of additional arguments to supply to the new instance.
Returns
Examples
// create a new instance of the factory, this is usually done by the class that will be using it. var factory = new FooGallery.utils.Factory(); // create a Logger class to register, this would usually be in another file var Logger = FooGallery.utils.Class.extend({ write: function( message ){ console.log( "Logger#write: " + message ); } }); factory.register( "logger", Logger ); // create a new instances of the class registered as "logger" var logger = factory.make( "logger" ); logger.write( "My message" ); // => "Logger#write: My message"
Details
-
names( [ prioritize ] ) → {Array.<string>}
Gets an array of all registered names.
-
Parameters
Name Type Attributes Default Description prioritize
boolean <optional> false Whether or not to order the names by the priority they were registered with.
Returns
Examples
// create a new instance of the factory, this is usually exposed by the class that will be using the factory. var factory = new FooGallery.utils.Factory(); // create some classes to register function Test1(){} function Test2(){} // register the classes with the factory with the default priority factory.register( "test-1", Test1 ); factory.register( "test-2", Test2, 1 ); // log all registered names console.log( factory.names() ); // => ["test-1","test-2"] console.log( factory.names( true ) ); // => ["test-2","test-1"] ~ "test-2" appears before "test-1" as it was registered with a higher priority
Details
-
register( name, klass [, priority ] ) → {boolean}
Registers a
klass
constructor with the factory using the givenname
. -
Description
Parameters
Name Type Attributes Default Description name
string The friendly name of the class.
klass
function The class constructor to register.
priority
number <optional> 0 This determines the index for the class when using either the load or names methods, a higher value equals a lower index.
Returns
Examples
// create a new instance of the factory, this is usually exposed by the class that will be using the factory. var factory = new FooGallery.utils.Factory(); // create a class to register function Test(){} // register the class with the factory with the default priority var succeeded = factory.register( "test", Test ); console.log( succeeded ); // => true console.log( factory.registered.hasOwnProperty( "test" ) ); // => true console.log( factory.registered[ "test" ].name === "test" ); // => true console.log( factory.registered[ "test" ].klass === Test ); // => true console.log( factory.registered[ "test" ].priority === 0 ); // => true
Details
-
<static> extend( [ definition ] ) → {function}
Creates a new class that inherits from this one which in turn allows itself to be extended.
-
Description
Parameters
Name Type Attributes Description definition
Object <optional> An object containing any methods to implement/override.
Returns
Examples
// create a base Person class var Person = FooGallery.utils.Class.extend({ construct: function(isDancing){ this.dancing = isDancing; }, dance: function(){ return this.dancing; } }); var Ninja = Person.extend({ construct: function(){ // Call the inherited version of construct() this._super( false ); }, dance: function(){ // Call the inherited version of dance() return this._super(); }, swingSword: function(){ return true; } }); var p = new Person(true); console.log( p.dance() ); // => true var n = new Ninja(); console.log( n.dance() ); // => false console.log( n.swingSword() ); // => true console.log( p instanceof Person && p.constructor === Person && p instanceof FooGallery.utils.Class && n instanceof Ninja && n.constructor === Ninja && n instanceof Person && n instanceof FooGallery.utils.Class ); // => true
Details
-
<static> override( name, fn )
Overrides a single method on this class.
-
Description
This is a helper method for overriding a single function of a FooGallery.utils.Class or one of its child classes. This uses the FooGallery.utils.fn.addOrOverride method internally and simply provides the correct prototype.
Parameters
Name Type Description name
string The name of the function to override.
fn
function The new function to override with, the
_super
method will be made available within this function.Examples
var Person = FooGallery.utils.Class.extend({ construct: function(isDancing){ this.dancing = isDancing; }, dance: function(){ return this.dancing; } }); var p = new Person(true); console.log( p.dance() ); // => true Person.override("dance", function(){ // Call the original version of dance() return !this._super(); }); console.log( p.dance() ); // => false
Details