UNPKG

de

Version:

Simple asynchronous promise-based controller

120 lines (92 loc) 1.97 kB
'use strict'; var Namespace; var Exception; var hasProperty; Exception = /** @type Exception */ require('./Exception'); Namespace = /** @type Namespace */ require('node-ns'); hasProperty = Object.prototype.hasOwnProperty; /** * @class Bundle * */ function Bundle () { /** * @public * @memberOf {Bundle} * @property {Object} * */ this.errors = {}; /** * @public * @memberOf {Bundle} * @property {Object} * */ this.result = {}; } Bundle.prototype = { /** * @property * @memberOf {Bundle} * * */ constructor: Bundle, /** * @public * @memberOf {Bundle} * @method * * @param {String} path * @param {*} error * */ setError: function (path, error) { if ( error instanceof Exception ) { throw error; } this._link(path, error, this.errors); }, /** * @public * @memberOf {Bundle} * @method * * @param {String} path * @param {*} result * */ setResult: function (path, result) { this._link(path, result, this.result); }, /** * @public * @memberOf {Bundle} * @method * * @returns {Array} * */ toArguments: function () { return [this.result, this.errors]; }, /** * @protected * @memberOf {Bundle} * @method * * @param {String} path * @param {*} data * @param {Object} root * * @returns {void} * */ _link: function (path, data, root) { var exists; exists = Namespace.useOn(root, path); if ( Object(exists) === exists ) { for ( path in data ) { if ( hasProperty.call(data, path) ) { exists[path] = data[path]; } } return; } Namespace.linkOn(root, path, data); } }; module.exports = Bundle;