Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>While Breeze team works on it, and if somebody needs this before it's done, here is code I wrote for making a copy of object and it's navigation properties:</p> <pre><code> function createSimpleObject(heavyObject) { if (heavyObject === undefined) return {}; var simpleObject = {}; manager.metadataStore._structuralTypeMap[heavyObject.entityType.name].dataProperties.forEach(function (singleProperty) { if (!singleProperty.isPartOfKey) simpleObject[singleProperty.name] = heavyObject[singleProperty.name](); }); return simpleObject; } function makeNavigationProperties(newObject, oldObject, navigationProperties) { if (oldObject === undefined || navigationProperties === undefined) return {}; navigationProperties.forEach(function (singleNavigationProperty) { var selectedArray = []; if (ko.isObservable(oldObject[singleNavigationProperty])) { selectedArray = oldObject[singleNavigationProperty](); } else selectedArray = oldObject[singleNavigationProperty]; if (selectedArray) { selectedArray.forEach(function (singleObject) { var simpleObject = {}; manager.metadataStore._structuralTypeMap[singleObject.entityType.name].dataProperties.forEach(function (singleProperty) { if (!singleProperty.isPartOfKey) { if (singleProperty.relatedNavigationProperty) { if (singleProperty.relatedNavigationProperty.entityTypeName === oldObject.entityType.name) { simpleObject[singleProperty.name] = newObject.id(); } else { if (ko.isObservable(singleObject[singleProperty.name])) simpleObject[singleProperty.name] = singleObject[singleProperty.name](); else simpleObject[singleProperty.name] = singleObject[singleProperty.name]; } } else { if (ko.isObservable(singleObject[singleProperty.name])) simpleObject[singleProperty.name] = singleObject[singleProperty.name](); else simpleObject[singleProperty.name] = singleObject[singleProperty.name]; } } }); manager.createEntity(singleObject.entityType.shortName, simpleObject); }); } }); } </code></pre> <p>and here is method that creates object:</p> <pre><code>function createMyObject(originalObject, navigationProperties){ var newMyObject = manager.createEntity('MyObject', createSimpleObject(originalObject)); makeNavigationProperties(newMyObject, originalObject, navigationProperties); return newMyObject; } </code></pre> <p>and finally code that calls creation of new object:</p> <pre><code>copiedObject(datacontext.createMyNewObject(originalObject(), ['navigationProperty1', 'navigationProperty2', 'navigationProperty3'])); </code></pre> <p>where copiedObject is observable containing new object, originalObject is object that I want to copy and 2nd parameter contains properties I want to copy. It only works for direct children of original object, I didn't need children of children so that is missing. It can be used with or without parameters so I use exactly the same function to create empty object or to copy entities with no children.</p> <p><strong>EDIT</strong></p> <p>Wards code works perfectly! Only one thing I'd change is his <code>fk</code> detection and use following to enable copying Junction tables:</p> <pre><code>var fk = false; navProp.entityType.foreignKeyProperties.forEach(function (singleProperty) { if (singleProperty.relatedNavigationProperty.entityTypeName == newItem.entityType.name) fk = singleProperty.name; }); if (!fk) return; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload