问题
I have a ViewModel that loads up 'child' entities and I want to also display 'grandchild' entities based off of each child that is loaded. To simplify things, I need help identifying how to query these objects and display them under the proper 'tree' in the browser (apologies for my butchering of coding language : ))
I am using Knockout to bind the data and loading up the entities with Breeze. This question is an extension of When to add extend additional complex types onto a Breeze entity
Also - my model is EF code first and I have a configuration defining a one to many relationship between children and grandchildren, and I think Breeze knows this already but I am trying to figure out how to take advantage of this.
childs.js (view model)
var childs = ko.observableArray();
var grandChilds = ko.observableArray();
var parentId = ko.observable();
function refresh() {
var parentId = (parent).parentId; // << for ex. don't worry about this line : )
return Q.all([getChildren(), getGrandChildren()]);
}
function getChildren() {
return datacontext.getChildren(childs, parentId);
}
function getGrandChildren() {
return datacontext.getGrandChildren(grandChilds);
}
and in the view (childs.html)
<div data-bind="foreach: childs">
<div title="Go to Child Details">
<div><strong data-bind="text: name"></strong></div>
<div><strong data-bind="text: gender().description"></strong></div>
</div>
<div>
<!-- ko.compose { view: grandChilds} --><!--/ko-->
</div>
</div>
And my current datacontext for reference
var getChilds= function (childsObservable, parentId) {
var query = EntityQuery.from('Childs')
.where('parentId', '==', parentId)
.expand('grandChildren')
.orderBy('id');
return manager.executeQuery(query)
.then(querySucceeded)
.fail(queryFailed);
function querySucceeded(data) {
if (childsObservable) {
childsObservable(data.results);
}
logger.log('Retrieved [Childs] and [Grandchilds] from remote data source', data, system.getModuleId(datacontext), true);
}
};
I want to load up only grandchildren of the children, and since there are many children I want to only display grandchildren under the correct child, not all grandchildren in one list. Any help would be appreciated.
回答1:
You have three basic approaches to querying and "linking" related entities.
1) Use EntityQuery.expand
2) Use EntityAspect.loadNavigationProperty
3) Create a "broad" EntityQuery that just happens to encompass the entire graph and all of the parent/child relations will be automatically linked.
来源:https://stackoverflow.com/questions/16200400/how-when-to-query-extended-entities-in-breeze