Methods
-
clear()
-
Clear all defined types and mixins
- Source:
-
define(type, definition) → {function}
-
Define new or inherited type
Parameters:
Name Type Argument Description type
string name of type
definition
DeclarationConf | DeclarationFn <optional>
see DeclarationConf or DeclarationFn. If it is null - delete declared object
- Source:
Returns:
constructor of defined object type
- Type
- function
Examples
var constructor = Moa.define('baseObj', { $ctor: function (name) { this.name = name; }, getName: function() { return this.name; } });
var constructor = Moa.define('child', function ($base) { // $base - containe reference to prototype of 'baseObj' return { $extend: 'baseObj', $ctor: function (name, age) { this.age = age; $base.$ctor.call(this, name); }, getAge: function () { return this.age; } }; });
Moa.define('base', {}); // new type declaration Moa.define('base', null); // delete type declaration
var childItem, base = Moa.define('base', function ($base) { // $base - undefined return { $ctor: function (name) { this.name = name; }, getName: function() { return this.name; } }; }), child = Moa.define('child', function ($base) { // $base - reference to 'base' type return { $extend: 'base', $ctor: function (name, age) { this.age = age; $base.$ctor.call(this, name); }, // override base implementation getName: function() { return 'Child: ' + $base.getName.call(this); }, getAge: function () { return this.age; } }; });
childItem = new child('Pet', 7); childItem.getName(); // 'Child: Pet' childItem.getAge(); // 7
var baseCtor, item, strMix = function () { this.add = function () { return (this.a.toString() + this.b.toString()); }; } base = { $ctor: function () { }, $static: { // Also you can declare static mixins in usual way $mixin: { str: 'strMix' }, getMsg: function () { return 'Static!'; }, a: 15, b: 17 } }; Moa.mixin('strMix', strMix); Moa.define('base', base);
baseCtor = Moa.define('base'); baseCtor.getMsg(); // 'Static!' - static method baseCtor.add(); // '15' + '17' => '1517' - static mixin Ctor.str.add.call(Ctor); // '1517'
var itemA, itemB, ItemC, singeltonConstructor = Moa.define('singleExample', { $single: true, $ctor: function () { this.name = 'Moa'; }, getName: function () { return this.name; } })
// Unfortunately it can not have constructor parameters itemA = new singeltonConstructor(); itemB = singeltonConstructor(); itemC = singeltonConstructor.getInstance(); // itemA equal itemB equal itemC
-
getRegistry() → {object}
-
Get all available types and mixins
- Source:
Returns:
object with arrays declared types and mixins
- Type
- object
Example
{ type: ['type1', 'type2', ...], mixin: ['mixin1', 'mixin2', ...] }
-
getTypeInfo(type) → {object}
-
Get internal information about type
Parameters:
Name Type Description type
string name of type
- Source:
Returns:
object with information about type, base type, applied mixins and configuration for dependency injection
- Type
- object
Example
{ $type: 'child', $basetype: 'base', $mixin: { mixA: 'mixinA', mixB: 'mixinB' }, $di: { $current: { type: 'child', instance: 'item', lifestyle: 'transient' }, $prop: { a: { type: 'base', instance: 'item', lifestyle: 'transient' }, b: 'child' } } }
-
mixin(mixType, definition)
-
Declare mixin
Parameters:
Name Type Description mixType
string name of mixin type
definition
function implementation of behavior for mixin. If it is null - delete declared mixin
- Source:
Examples
var numMix = function () { this.add = function () { return (this.a + this.b); }; this.sub = function () { return (this.a - this.b); }; this.mul = function () { return (this.a * this.b); }; }; Moa.mixin('numMix', numMix);
var item, Ctor, base = { $ctor: function (a, b) { this.a = a; this.b = b; }, $mixin: { nummix: 'numMix' }, mul: function () { return 'a*b=' + this.nummix.mul.call(this); } }; Ctor = Moa.define('base', base); item = new Ctor(3, 4); item.add(); // 7 item.mul(); // 'a*b=12'
var Ctor, item, base = { $ctor: function (a, b) { this.a = a; this.b = b; }, $mixin: { num: 'numMix', str: 'strMix' } }, numMix = function () { this.add = function () { return (this.a + this.b); }; }, strMix = function () { this.add = function () { return (this.a.toString() + this.b.toString()); }; }; Moa.mixin('numMix', numMix); Moa.mixin('strMix', strMix); Ctor = Moa.define('base', base); item = new Ctor(10, 12); item.add(); // '1012' item.num.add.call(item); // 22 item.str.add.call(item); //'1012'
Moa.mixin('mix', function () {}); // new mixin declaration Moa.mixin('mix', null); // delete mixin declaration
var base = { $mixin: { num: 'numMix' }, $static: { $mixin: { str: 'strMix' } } }
-
resolve(type, paramsObj) → {object}
-
Resolve new instance of type with field and constructor injection. Resolving logic based on $di configuration of type declaration.
Parameters:
Name Type Argument Description type
string name of type
paramsObj
object <optional>
constructor parameter for resolved type
- Source:
Returns:
instance of type
- Type
- object
Type Definitions
-
DeclarationConf
-
Declaration configuration for type
Type:
- object
- Source:
Properties:
Name Type Argument Description $ctor
function <optional>
constructor of type
$extend
string <optional>
inheritable type name
$di
DiConf <optional>
configuration for IoC container
$mixin
object <optional>
literal with mixins declaration
$static
object <optional>
literal with properties and function that applied to constructor
$single
boolean <optional>
setup type as singleton
-
DeclarationFn($base) → {DeclarationConf}
-
Declaration configuration for type
Parameters:
Name Type Description $base
object prototype of inheritable type with $base.$ctor - constructor of inheritable type
- Source:
Returns:
object that uses $base closure for access to inheritable type implementation constructor and methods
- Type
- DeclarationConf
-
DiConf
-
Configuration of dependency injection. Used as $di parameter in type declaration.
Type:
- object
- Source:
Properties:
Name Type Argument Description $current
object <optional>
set default injection behavior for declared type
$ctor
object <optional>
literal declare types that inject to constructor
$prop
object <optional>
literal declare types that inject to instance properties
$proto
object <optional>
literal declare types that inject to prototype of instance properties. BE CAREFUL! It resolved one time after use 'resolve' method and override exist properties and methods in prototype. Resolved properties and methods available in prototype of constructor type for all places where constructor uses ('define' method for example).
* properties that injected as instance properties. All string values try to resolve as declared types
Example
{ $ctor: { fieldA: { type: 'typeA', instance: 'item', lifestyle: 'transient' }, field: 'typeB' // try to resolve as 'typeB' otherwise use as a string }, $prop: { propA: 'typeA' // resolved like fieldA to instance field }, $proto: { protoProp: { // resolve constructor of 'typeB' to instance prototype type: 'typeB', instance: 'ctor' // if instance is 'item' it has lifestyle as 'singleton' } }, propC: { // resolve the same instance of 'typeC' for every instance in $prop literal type: 'typeA', instance: 'item', lifestyle: 'singleton' }, prop: 2315 // copy number field to resolved instance }
-
InjectionConf
-
Declaration of dependency injection behavior
Type:
- object
- Source:
Properties:
Name Type Description type
string name of type for injection. Not available for $current in DiConf
instance
string Injected instance. Values: 'item' or 'ctor'. Default value: 'item'.
lifestyle
string Life style for 'item' instance. Not used for 'ctor'. Values: 'transient' or 'singleton'. Default value: 'transient'.