Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI appreciate the issue and your diligent attempt to solve it. Before digging deeply, I'd like to steer you away from one thing in particular ... a reference to any Breeze variable that begins "_". The "_" prefix means that the variable is internal to Breeze and is not intended for you to read or write. It is a private implementation artifact and you'd never see if it JavaScript would let us hide it. We could make it disappear tomorrow. In this case, you have the perfectly good alternative, `getEntityType`
      singulars
    2. COSecond, if you dig into the Breeze serialization logic, you'll see why I recommended the `exportEntities` method. Cloning is not as simple as copying dataProperties. Part of your copy example involves coping reference navigation property values; I'd rather you let Breeze do that.
      singulars
    3. COI know using variables like `_structuralTypeMap` is bad. I know going manually into navigation properties is also bad. On the other hand, copying some reference navigation properties are there for some junction tables. Model is actually built so this can be done without breaking it, and also why I ended up using 2nd parameter with array of navigation properties to be copied. I see why using the export entities could work, but I'll put that on TODO list for later when time opens up. Thank you! (P.S. all this is reason why methods have "DO NOT TOUCH, YOU WILL BREAK IT" comments before it)
      singulars
 

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