【发布时间】:2014-09-24 23:13:32
【问题描述】:
为了确保我正在访问当前视图,我可以这样做:
function attached(view, parent) {
$("#element",view).hide();
}
这将返回一个undefined
viewModel.selectedcategory.subscribe(function (data,view) {
console.log(data,view);
$("#element",view).hide();
});
所以我访问视图的方法是,我定义了一个全局变量currentView,如下所示:
var currentView,
viewModel = {
activate: activate,
attached: attached,
selectedcategory: ko.observable(false)
}
viewModel.selectedcategory.subscribe(function (callback) {
console.log(callback, activeView);// now i can access the view
$("#element",activeView).hide();
});
return viewModel;
function attached(view, parent) {
currentView = view; // update currentView
$("#element",view).hide();
}
有没有更好的方法来像订阅一样做同样的事情
viewModel.selectedcategory.subscribe(function (subscribeView, subscribeParent) {
});
【问题讨论】:
标签: knockout.js durandal