这不起作用,它将defaultView设置为“bar”,但是当ajax请求完成时,永远不会更新为“foo”。我试图将它设置为dependentObservable,但没有更多的运气。我错过了一些明显的东西,还是我试图将命名空间拉得太远?
$.get("/players/summary", function(json) {
var playerViewModel = ko.mapping.fromJS(json);
dateViewModel.defaultDate = "foo";
ko.applyBindings(playerViewModel, 'player');
});
var dateViewModel = {
defaultDate: ko.observable("bar")
};
ko.applyBindings(dateViewModel, 'date');
答案 0 :(得分:2)
您没有正确设置“foo”。你应该写:
dateViewModel.defaultDate("foo");
所以你有:
$.get("/players/summary", function(json) {
var playerViewModel = ko.mapping.fromJS(json);
dateViewModel.defaultDate("foo"); // <--- Set foo using getter setter syntax
ko.applyBindings(playerViewModel, 'player');
});
var dateViewModel = {
defaultDate: ko.observable("bar")
};
ko.applyBindings(dateViewModel, 'date');
这应该可以解决问题,但是如果您仍然遇到问题,我会在dateViewModel
来电之前确定$.get
。{/ p>