d2-ui
Version:
121 lines (102 loc) • 4.63 kB
JavaScript
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _libCheck = require('../lib/check');
/**
* @class ModelDefinitions
*
* @description
* Contains all the `ModelDefinition`s that are available. The definitions are properties on the object.
* This would be used as a main entry point to do any interaction.
*
* After calling the initialise function `d2({baseUrl: 'dhis/api'})` this object is the `models` property
* that allows you to access
*
* ```js
* models.dataElement.getList();
* ```
*/
var ModelDefinitions = (function () {
function ModelDefinitions() {
_classCallCheck(this, ModelDefinitions);
}
// Model definitions singleton!
_createClass(ModelDefinitions, [{
key: 'add',
// TODO: Elaborate this documentation
/**
* @method add
* @param {ModelDefinition} modelDefinition Add a model definition to the definitions collection
*
* @description
* This will allow you to add your own custom ModelDefinitions.
*
* The Definition object should have the following properties
* `modelName, modelNamePlural, modelOptions, properties, validations`
*
* ```js
* models.add({name: 'MyDefinition', plural: 'MyDefinitions', endPointname: '/myDefinition'});
* ```
*/
value: function add(modelDefinition) {
try {
(0, _libCheck.checkType)(modelDefinition.name, 'string');
} catch (e) {
throw new Error('Name should be set on the passed ModelDefinition to add one');
}
if (this[modelDefinition.name]) {
throw new Error(['Model', modelDefinition.name, 'already exists'].join(' '));
}
this[modelDefinition.name] = modelDefinition;
if ((0, _libCheck.isType)(modelDefinition.plural, 'string')) {
this[modelDefinition.plural] = modelDefinition;
}
}
/**
* @method mapThroughDefinitions
*
* @param {Function} transformer Transformer function that will be run for each `ModelDefinition`
* @returns {Array} Array with the `ModelDefinition` objects.
*
* @description
* Map through the modelDefinitions like you would with a simple `Array.map()`
*
* ```js
* models.mapThroughDefinitions(definition => console.log(definition.name);
* ```
*
* @note {info} When mapping through the definition list `transformer` is called with the just the definition
* Unlike other map functions, no index or the full object is being passed.
*
* @note {warn} The resulting array contains references to the actual objects. It does not work like immutable array functions.
*
*/
}, {
key: 'mapThroughDefinitions',
value: function mapThroughDefinitions(transformer) {
var result = [];
var modelDefinition = undefined;
(0, _libCheck.checkType)(transformer, 'function', 'transformer');
for (modelDefinition in this) {
if (this.hasOwnProperty(modelDefinition) && !(this[modelDefinition].plural === modelDefinition)) {
result.push(transformer(this[modelDefinition]));
}
}
return result;
}
}]);
return ModelDefinitions;
})();
function getModelDefinitions() {
if (getModelDefinitions.modelDefinitions) {
return getModelDefinitions.modelDefinitions;
}
return getModelDefinitions.modelDefinitions = new ModelDefinitions();
}
ModelDefinitions.getModelDefinitions = getModelDefinitions;
exports['default'] = ModelDefinitions;
module.exports = exports['default'];
//# sourceMappingURL=ModelDefinitions.js.map
;