Reputation: 41
I am returning an object that contains nested models via breeze. Let me take the canonical sample. Customers and Orders. I am using EF5 on the server. Using Breeze, I am calling my controller to get all customers and am also including the ".expand" attribute in the breeze query call to include the "Orders" entity also. The json comes back perfectly and I am using the "kendo.breeze.datasource" built by "Derick Bailey" to map breeze entity to knockout observables [using ko.mapping] and then eventually bind to kendo grid. This works perfectly until we want to return nested types. For ex, I want to bind, "order().Orderid" to an element. so, when I try to pass the "data" to kendo datasource to bind to grid, I get a "Stack overflow" exception. This scenario was handled for "entityAspect" and "entityType" recursion by the kendo.breeze.datasource module but obviously it cannot handle custom nested types. I tried to "ignore" the property "orders().customer" in ko.mapping.toJs to prevent "order" referencing back to customer but that's not ignoring properties at the child level. Of course, if I ignore "orders" completely, the binding works but I can't access "order().Orderid" in the grid. help pls?
Upvotes: 2
Views: 1340
Reputation: 810
What you need to handle on the return of the breeze data - prior to passing onto the grid - use ko.mapping to ignore ['entityAspect', 'entityType', and 'orders']
I ran into a very similar problem, and had started off with "kendo.breeze.datasource" by Derick over at kendo-labs as well. I started keno-breeze-knockout to build out something a bit more...
Part of the sample that does what your after is:
new kendo.data.extensions.BreezeDataSource({
entityManager: self.datacontext.manager,
endPoint: new breeze.EntityQuery.from('Products').expand('Category'),
defaultSort: "productName asc",
mapping: {
ignore: ['products'] // category.products is recursive - ignore it
}
The source that should highlight what your after is this:
``` function querySucceeded(xhr) { payload.data = self.mapping.mapToJS(xhr.results);
if (self.inlineCount) {
payload.total = xhr.inlineCount;
}
options.success(payload); // notify the DataSource that the operation is complete
return true;
}
```
function mapToJS(items) {
baseIgnore: ['entityType', 'entityAspect'],
include: [],
ignore: [], // set your particular properties here
mapToJS : function(results) {
var koMapping = {
'include': this.include,
'ignore': this.baseIgnore.concat(this.ignore)
};
// if you override - you might not care about using ko.mapping?
if (!ko.mapping) {
throw new Error('knockout mapping plugin is required!');
}
return ko.mapping.toJS(results,koMapping);
}
}
Upvotes: 0