Methods
-
clear()
-
Clear all defined types and mixins
- Source:
-
define(type, definition) → {function}
-
Define new or inherited type
Parameters:
Name Type Argument Description typestring name of type
definitionDeclarationConf | DeclarationFn <optional>
see DeclarationConf or DeclarationFn. If it is null - delete declared object
- Source:
Returns:
constructor of defined object type
- Type
- function
Examples
Declaration without
$baseclosurevar constructor = Moa.define('baseObj', { $ctor: function (name) { this.name = name; }, getName: function() { return this.name; } });Declaration inheritance and
$baseclosurevar 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; } }; });Delete type declaration
Moa.define('base', {}); // new type declaration Moa.define('base', null); // delete type declarationDeclaration
$baseclosurevar 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; } }; });Using instance
childItem = new child('Pet', 7); childItem.getName(); // 'Child: Pet' childItem.getAge(); // 7Declaration static methods
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);Using static methods
baseCtor = Moa.define('base'); baseCtor.getMsg(); // 'Static!' - static method baseCtor.add(); // '15' + '17' => '1517' - static mixin Ctor.str.add.call(Ctor); // '1517'Declaration singleton
var itemA, itemB, ItemC, singeltonConstructor = Moa.define('singleExample', { $single: true, $ctor: function () { this.name = 'Moa'; }, getName: function () { return this.name; } })Using singleton
// 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 typestring 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 mixTypestring name of mixin type
definitionfunction implementation of behavior for mixin. If it is null - delete declared mixin
- Source:
Examples
Declaration mixin
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);Using mixin
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'Multiple mixins example
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'Delete mixin declaration
Moa.mixin('mix', function () {}); // new mixin declaration Moa.mixin('mix', null); // delete mixin declarationStatic 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 typestring name of type
paramsObjobject <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 $ctorfunction <optional>
constructor of type
$extendstring <optional>
inheritable type name
$diDiConf <optional>
configuration for IoC container
$mixinobject <optional>
literal with mixins declaration
$staticobject <optional>
literal with properties and function that applied to constructor
$singleboolean <optional>
setup type as singleton
-
DeclarationFn($base) → {DeclarationConf}
-
Declaration configuration for type
Parameters:
Name Type Description $baseobject 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 $currentobject <optional>
set default injection behavior for declared type
$ctorobject <optional>
literal declare types that inject to constructor
$propobject <optional>
literal declare types that inject to instance properties
$protoobject <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 typestring name of type for injection. Not available for $current in DiConf
instancestring Injected instance. Values: 'item' or 'ctor'. Default value: 'item'.
lifestylestring Life style for 'item' instance. Not used for 'ctor'. Values: 'transient' or 'singleton'. Default value: 'transient'.