1

我所需要的只是监视当前路线,以便一些计算出的 observables 相应地更新。相关代码和注释中的解释:

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');
    }

任何帮助将非常感激。整个周末我都在为此头疼。

4

1 回答 1

2

这不是您的问题Durandal:确定视图当前是否处于活动状态(可能使用路由器)的变体吗?. 同样的答案也适用于此。

假设您的外壳返回一个单例,如下所示应该可以满足您的需求。

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,
    ...
   }
});
于 2013-06-03T08:00:22.300 回答