2013-06-03 4 views
1

필자가 필요로하는 것은 현재 계산 된 관측치가 적절하게 업데이트되도록 현재 경로를 모니터링하는 것입니다. 의견에 대한 설명이있는 관련 코드 :Durandal shell에서 경로를 모니터링하는 방법은 무엇입니까?

define(['durandal/plugins/router', 'durandal/app','services/dataservice'], function (router, app, dataservice) { 

    // TODO: I need to make these computed observables that monitor the route, but I can't monitor the route until after it's been activated, so I just define these guys as observables here 
    this.isViewingFolder = ko.observable(); 
    this.isViewingSet = ko.observable(); 

    // These don't evaluate correctly because isViewingFolder() doesn't get defined until after the DOM loads (I think) 
    var displayAddForFolder = ko.computed(function() { 
     return selectedItemsCount() == 0 && isViewingFolder() && !isViewingSet(); 
    }); 
    var displayAddForSet = ko.computed(function() { 
     return selectedItemsCount() == 0 isViewingSet() && !isViewingFolder(); 
    }); 

    // The ONLY way I've been able to get this to work is with the following (putting a computed into an observable??) 
    var viewAttached = function() { 
     isViewingFolder(ko.computed(function() { 
      return router.activeRoute().moduleId == 'viewmodels/folder'; 
     })); 
     isViewingSet(ko.computed(function() { 
      return router.activeRoute().moduleId == 'viewmodels/set'; 
     })); 
    } 

    // If I try this the value updates once but only once, never again: 
    var viewAttached = function() { 
     isViewingFolder(router.activeRoute().moduleId == 'viewmodels/folder'); 
     isViewingSet(ko.computed(router.activeRoute().moduleId == 'viewmodels/set'); 
    } 

도움이 될만한 정보가 있습니다. 주말 내내 내 머리를 때렸어.

답변

2

이 질문은 유사 변형되지 않습니다. Durandal: Determining if a view is currently active (possibly using Router)? 같은 대답이 여기에 적용됩니다.

쉘이 다음과 같은 싱글 톤을 반환한다고 가정하면 원하는 것을 할 수 있습니다.

define(['durandal/plugins/router', 'durandal/app','services/dataservice'], function (router, app, dataservice) { 

var isViewingFolder = ko.observable(false); 
var isViewingSet = ko.observable(false); 

router.activeRoute.subscribe(function(val) { 
     isViewingFolder(val.moduleId === 'viewmodels/folder'); 
     isViewingSet(val.moduleId === 'viewmodels/set'); 
     console.log('isViewingFolderOrSet', isViewingFolder(), isViewingSet()); 

    }); 
... 


return { 
    router: router, 
    isViewingFolder: isViewingFolder, 
    isViewingSet: isViewingSet, 
    ... 
    } 
}); 
+0

저는 어떻게 작동시키는 지 알 수 없습니다. Rainer 감사합니다. – RobVious

관련 문제