2013-06-17 5 views
3

breeze의 새로운 기능 - 일대 다 관계로 배열 탐색 속성이있는 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; 
} 

답변

2

비동기 쿼리가 완료되기 전에 childrenExist를 반환하기 때문에 현재 원하는 것을 반환하지 않는 이유가 있습니다. .then() 함수를 호출하면 콜백을 잡아 내고 데이터가 검색되면 IT 콜백 만 반환합니다. 실패하지 않는 한 오류를 반환하거나 단순히 '거짓'을 반환 할 수 있습니다.

var showChildren = ko.observable(false); // Do something like hide children until they exist 

function refresh(entity) { 
    doChildrenExist(entity) 
     .then(checkResult)    
} 

function checkResult(result) { 
    showChildren(result); 
} 
3

이 더 있습니다 - doChildrenExist

function doChildrenExist (entity) { 
    // No need to define a variable since we are either returning true or false 
    entity.entityAspect.loadNavigationProperty("children") 
    .then(function() { 
      if(entity.children().length > 0) { 
       return true; 
      } 
      return false; }) 
    .fail(catchError); 

    function catchError(e) { 
     alert(e.message); 
     return false; 
    } 
} 

및 함수에 호출되는 -

function doChildrenExist (entity) { 

    var childrenExist = false; 

    entity.entityAspect.loadNavigationProperty("children") 
    .then(function() { 
      if(entity.children().length > 0) { 
       childrenExist = true; 
      } 
      return childrenExist; }) 
    .fail(catchError); 

    function catchError(e) { 
     alert(e.message); 
     return false; 
    } 
} 

는 여러분이이 작업을 수행 할 수 있어야한다 코드 몇 바이트를 저장할 수 없습니다 방법은 동 기적으로 탐색 속성을로드하는 것입니다 (이것은 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() { 
     ... 
    }); 
}