I have been banging my head for several days.
Imagine you have a car sales management application. You sell different models. Your car model has 50 objects. For example, suppose you want to sell a Bugatti Veyron. Now you have just received 5 of these cars. So, I enter my application, create the first Bugatti Veyron with a specific identifier. Then I want to add a second one, but there is a problem - I will have to write down all these properties again! I would like to have a Copy button, and I would just change the serial number, the breeze would change the identifier and voila, two cars there!
For hacking, I first created this solution:
newCar(datacontext.createCar());
newCar().property1(oldCar().property1());
newCar().property2(oldCar().property2());
...
It was ugly, and after I proved that I can do it, of course, the request for the application was to do everything with the ability to copy - I would not do that! There must be a copy somewhere. After digging through many things, even trying to change some things in the wind, I could not do something like:
manager.createEntity('Car', oldCar);
Now the last solution is a little more viable than the first, but still requires more code than I would like, and not as intuitively as it could be:
var newObject = {};
manager.metadataStore._structuralTypeMap[oldCar.entityType.name].dataProperties.forEach(function (singleProperty) {
if (!singleProperty.isPartOfKey)
newObject[singleProperty.name] = oldCar[singleProperty.name];
});
var newCar = manager.createEntity('Equipment', newObject);
Is there another “clean” way to create a new object with exactly the same properties, but, of course, a different identifier?
I should mention that there are some ICollections in the Car object, but this hack-ish solution ignores them, which can be improved, but I am currently processing it myself with several for.Ee loops.