UNPKG

mangopay4-nodejs-sdk

Version:

Mangopay Node.js SDK

70 lines (62 loc) • 2.24 kB
const _ = require('underscore'); const Model = require('../Model'); const EntityBase = Model.extend({ defaults: { Id: null, Tag: null, CreationDate: null }, getReadOnlyProperties: function() { const properties = Model.prototype.getReadOnlyProperties(); properties.push('Id', 'CreationDate'); return properties; }, /** * Casting the entity to JSON data that will be passed to the server */ toJSON: function() { const self = this; /** * Flatten object model structure */ _.each(this, function(dataValue){ if (dataValue instanceof Model) { _.extendOwn(self, dataValue); } }); /** * Parse dependents object functionality */ const dependsObjects = this.getDependsObjects(); if (dependsObjects.length) { _.each(dependsObjects, function(dependsObject){ const propertyValue = self.getData(dependsObject.propertyName); _.each(dependsObject.propertyValueMapping, function(modelClass, key) { if (propertyValue instanceof modelClass) { self[dependsObject.dependsPropertyName] = key; } }); }); } // delete self.CreationDate; return self; }, /** * Parse method is being called to interpret the data received from the server */ parse: function() { const self = this; const dependsObjects = this.getDependsObjects(); if (dependsObjects.length) { _.each(dependsObjects, function(dependsObject){ const dependsPropertyValue = self.getData(dependsObject.dependsPropertyName); const PropertyObjectClass = dependsObject.propertyValueMapping[dependsPropertyValue]; if (PropertyObjectClass) { const updatedDependsObject = new PropertyObjectClass(self.getData(dependsObject.propertyName)); self.setData(dependsObject.propertyName, updatedDependsObject); } }); } } }); module.exports = EntityBase;