I'm trying to lazy load a complex type in breeze but cannot figure out a way to accomplish this.
The reason I would like to use a complex type and not the navigation way, is the service I have to consume is not doing CRUD the same way as breeze. I have to send one single object with all its subobjects (both scalar and non scalar) to a single service method responsible for storing data (insert/update/delete).
I tried to do this with navigation properties but this means I have to create an array of entities to send to an API controller, and there recreate the entire object. This would be difficult, but even more so because there are no foreign keys in the child objects (which is the case in all samples I've seen until now) making it a pain to map them again.
With complex types, I don't have this issue (or not that I'm aware of).
I have to consume an object structure like this:
1.Parent: product (class)
1.1Child: packages (array)
1.2Child: splitLevels (array)
1.2.1Grandchild: permissions (array)
1.2.1.1Grandgrandchild: pharmacies (array)
1.2.2Grandchild: splitLevel (class)
Packages are loaded together with the product, this works just fine. Howevers, splitLevels are not included in this datacontract (since it requires too much data and will not be consulted that often). When requesting this data, a boolean is added to the product to indicate they have been loaded, from then on it is required to send them to the server as well.
When loading product, this results in an issue: Object doesn't support property or method 'getProperty'
This is caused by the _initializeInstance method in breeze:
if (initFn) {
if (typeof initFn === "string") {
initFn = instance[initFn];
}
initFn(instance);
}
this.complexProperties && this.complexProperties.forEach(function (cp) {
var ctInstance = instance.getProperty(cp.name);
cp.dataType._initializeInstance(ctInstance);
});
The instance is empty, no properties can be fetched from it.
Is there any way to work around this? Is there a way to use navigation properties without getting multiple entities; so I can send a single object without using this:
if (product.entityAspect.entityState.isUnchanged()) {
product.entityAspect.setModified();
}
// Packages
var entitiesToSave = product.packages().slice();// copy
// Split Levels
if (product.storeSplitLevels) {
product.splitLevelsLoaded(true);
// TODO: Add split levels to entities to save
}
// Product Details
entitiesToSave.push(product);