如何将可观察对象从一个命名空间视图模型更新到另一个

发布于 2024-12-22 04:55:31 字数 443 浏览 1 评论 0原文

这不起作用,它将 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');

This does not work, it sets the defaultView as "bar", but then never updates to "foo" when the ajax request is completed. I tried to set this as a dependentObservable but didn't have any more luck. Am I missing something obvious or am I trying to stretch namespaces too far?

$.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');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空袭的梦i 2024-12-29 04:55:31

您没有正确设置“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'); 

这应该可以解决它,但如果你仍然有问题,那么我会在你的 $.get 调用之前看看定义 dateViewModel

You are not setting "foo" correctly. You should write:

dateViewModel.defaultDate("foo"); 

So you have:

$.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'); 

This should fix it, but if you still have problems then I would look at definining dateViewModel before your $.get call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文