我所需要的只是监视当前路线,以便一些计算出的 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');
}
任何帮助将非常感激。整个周末我都在为此头疼。