The Context itself
not allowed to use as a function
@param {Object} config The object that is the source for the Context.
function Context(config) {
var self = this;
if (!(self instanceof Context)) {
throw new Error('constructor is not a function');
}
self.overwrite(config);
}
Used to apply changes to context;
Context.prototype.overwrite = function(config) {
var self = this;
if (config) {
var val;
for (var prop in config) {
val = config[prop];
if (!reserved[prop]) {
if (val !== undefined && val !== null)
self[prop] = config[prop];
}
}
}
};
Returns list of child contexts.
Context.prototype.getChilds = function() {
var self = this;
if (!self.__children) {
self.__children = [];
}
return self.__children;
};
Return parent Context
Context.prototype.getParent = function() {
var self = this;
return self.__parent;
};
checks wheater or not context has specific child context
it return true
also if ctx
is self
;
Context.prototype.hasChild = function(ctx) {
var self = this;
if (ctx instanceof Context) {
return ctx.__parent === self || self === ctx;
}
};
Option name | Type | Description |
---|---|---|
ctx | Object,Context | verified context |
return |
static function which ensures that the object is proper Type
Context.ensure = function(ctx) {
if (!(ctx instanceof Context)) {
return new Context(ctx);
} else {
return ctx;
}
};
Option name | Type | Description |
---|---|---|
ctx | Object,Context | |
return | Context |
Ensures that the context is the child of current context, and returns right context
Context.prototype.ensureIsChild = function(ctx) {
var self = this;
var lctx = Context.ensure(ctx);
if (!self.hasChild(lctx)) {
self.addChild(lctx);
}
return lctx;
};
Option name | Type | Description |
---|---|---|
ctx | Context | new child context |
Add child Context to current
!Note! All children contexts has parent list of error. This allow to be sure that any fork
Context.prototype.addChild = function(ctx) {
var self = this;
if (!self.hasChild(ctx)) {
var child = Context.ensure(ctx);
child.__parent = self;
if (!self.__children) {
self.__children = [];
}
self.__children.push(child);
}
};
Option name | Type | Description |
---|---|---|
[config] | Object,Context | new properties that must exists in new fork |
Makes fork of current context and add it to current as a child context
Context.prototype.fork = function(config) {
var self = this;
var child = new Context(self);
self.addChild(child);
for (var p in config) {
child[p] = config[p];
}
return child;
};
Option name | Type | Description |
---|---|---|
String | at | path to context object that need to be a Context instance |
return | Context |
Same but different as a fork. it make possible get piece of context as context;
Context.prototype.get = function(path) {
var root = get(this, path);
if (root instanceof Object) {
var result = root;
if (!(result instanceof Context)) {
result = this.ensureIsChild(result);
set(this, path, result);
}
return result;
}
};
Option name | Type | Description |
---|---|---|
[clean] | Boolean |
|
return | Object |
Convert context to raw Object;
Context.prototype.toObject = function(clean) {
var self = this;
var obj = {};
for (var p in self) {
if (!reserved[p]) {
obj[p] = clone(self[p], clean);
}
}
return obj;
};
Conterts context to JSON
Context.prototype.toJSON = function() {
var self = this;
// always cleaning the object
return JSON.stringify(self.toObject(true));
};