breeze 的新手 - 我有一个 breeze 实体,它有一个一对多关系的数组导航属性。我想检查父实体是否在导航属性中存在任何相关的子实体。这个子数组还没有展开,我想懒加载检查一下。
由于加载是异步发生的,看起来它可能不会被检查加载 (如果 entity.children()...)。 如果我将支票放在“then”回调中,似乎我也会遇到同样的问题。有没有办法同步加载子数组,以便我可以检查并返回是否填充?或者有更好的方法吗?
function doChildrenExist (entity) {
var childrenExist = false;
entity.entityAspect.loadNavigationProperty("children")
.then(function () {})
.fail(function () {});
if(entity.children() !== null || entity.children().length > 0) {
childrenExist = true;
}
return childrenExist;
}
最佳答案
没有办法同步加载导航属性(这是 ajax 的本质),但您在 loadNavigationProperty 的正确轨道上。您可以执行类似下面的操作,在其中传入一个回调函数,该函数将根据子项是否存在以“true”或“false”调用。
function doChildrenExist(entity, callback) {
entity.entityAspect.loadNavigationProperty("children").then(function () {
// this syntax assumes knockout model library.
var childrenExist = entity.children().length > 0;
callback(childrenExist);
}).fail(function () {
...
});
}
关于javascript - Breeze - 延迟加载导航属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17150852/