Skip to content

Instantly share code, notes, and snippets.

@johnpapa
Created February 12, 2013 15:51
Show Gist options
  • Save johnpapa/4770826 to your computer and use it in GitHub Desktop.
Save johnpapa/4770826 to your computer and use it in GitHub Desktop.
Partial Entity Mapper. Now accepts the key name (only supports single names) and the additional object property and value pairs you want to map. The latter defaults to {isPartial:true}
define(
function () {
var defaultExtension = { isPartial: true };
var mapper = {
mapDtosToEntities: mapDtosToEntities
};
return mapper;
/**
Map an array of DTO's for a type of entity
to an array of Breeze entities that are managed by
the entity manager and are observables.
@method mapDtosToEntities
@param manager {Object} The breeze manager instance
@param dtos {Object|Array of Object} The objects to map to entities
@param entityName {String} The name of the entity
@param keyName {String} The name of the entity key property
@param mapping {Object=defaultMapping} Object containing the default mapping for the entity
@returns {Object} breeze entity
**/
function mapDtosToEntities(manager, dtos, entityName, keyName, extendWith) {
return dtos.map(dtoToEntityMapper);
function dtoToEntityMapper(dto) {
var keyValue = dto[keyName];
var entity = manager.getEntityByKey(entityName, keyValue);
if (!entity) {
// We don't have it, so create it as a partial
extendWith = $.extend({ }, extendWith || defaultExtension);
extendWith[keyName] = keyValue;
entity = manager.createEntity(entityName, extendWith);
}
mapToEntity(entity, dto);
entity.entityAspect.setUnchanged();
return entity;
}
function mapToEntity(entity, dto) {
// entity is an object with observables
// dto is from json
for (var prop in dto) {
if (dto.hasOwnProperty(prop)) {
entity[prop](dto[prop]);
}
}
return entity;
}
}
});
@ibrahimahmed-io
Copy link

Great library, thanks john

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment