我试图做一些按钮隐形基于用户是否是的loggedIn与否,为了这个,我已经做了类似:淘汰赛可见绑定失败
我有一个名为authmodule模块,编码为:
define(function() {
var loggedIn = ko.observable(false); // tried without observable too.
var updateLoginStatus = function() {
// call the webapi using ajax and intercept the 401
// if error is 401 set set loggedIn to false and
// true otherwise.
// set ajax call
var options = {
url: '/breeze/breeze/MicCheck123',
type: 'GET',
dataType: 'json'
};
// make call
return $.ajax(options)
.then(function (data) {
loggedIn(true);
})
.fail(function (jqXhr, textStatus) {
if (jqXhr.status == 401 || jqXhr.status == 404) {
loggedIn(false);
}
});
};
// my ko.computed used in html to do visible binding
var isUserLoggedIn = ko.computed(function() {
updateLoginStatus();
return loggedIn;
});
var authmodule = {
isUserLoggedIn: isUserLoggedIn,
updateLoginStatus: updateLoginStatus
};
return authmodule;
});
我现在需要这个authmodule在我shell.js,也返回从viewmodle相同如下图所示
define(['durandal/system',
'services/logger',
'durandal/plugins/router',
'config',
'services/authmodule'],
function (system, logger, router, config, authmodule) {
var shell = {
activate: activate,
authmodule: authmodule,
router: router
};
return shell;
function activate() {
return boot();
}
function boot() {
router.map(config.routes);
return router.activate(config.startModule);
}
}
);
和相应shell.js的HTML LOO KS如下
<div class="navbar-inner navbar-secondary">
<div class="btn-group">
<!-- ko foreach: router.visibleRoutes -->
<a data-bind="attr: { href: hash },
css: { active: isActive },
html: caption,
visible: $parent.authmodule.isUserLoggedIn"
class="btn btn-info"></a>
<!-- /ko -->
</div>
</div>
因为我有四个明显的路线,我希望看到在顶部功能区4个按键,当阿贾克斯成功,当阿贾克斯未能我希望看到没有任何按键,但是这似乎并没有奏效不管ajax结果如何。
有人可以帮助我确定我错过了什么吗?
我已经看过
Knock out visible binding of anchor tag is not working
你确定$ parent'会给你正确的上下文 –