为什么我的淘汰赛观察结果没有更新?

发布于 2024-12-21 15:03:08 字数 1027 浏览 1 评论 0原文

我的 html 在页面初始化时显示“占位符”,但随后我的 ajax 调用没有更新。是否存在某种范围界定问题?

var currentPlayer = {
  "player_id": "placeholder"
};

$.ajax({
  url: "/players/summary",
  success: function(json) {
    // when this ajax call is completed the placeholder string is not replaced by
    // currentPlayer.player_id.  I have verified that the currentPlayer hash does
    // have that value after this ajax call
    currentPlayer = json;
  }
});

var dashboardViewModel = {
  // <span data-bind="text:currentPlayer"></span> displays the "placeholder"
  // string upon initialization
  currentPlayer: ko.observable(currentPlayer.player_id),
  vsPlayer: ko.observable("VS: ALL PLAYERS")
};

ko.applyBindings(dashboardViewModel);

编辑:

这就是我解决问题的方法:

var dashboardViewModel = {
  currentPlayer: ko.observable({"player_id": ""}),
  vsPlayer: ko.observable("VS: ALL PLAYERS")
};

ko.applyBindings(dashboardViewModel);

$.get("/players/summary", dashboardViewModel.currentPlayer);

My html is showing "placeholder" upon initialization of the page, but then is not getting updated by my ajax call. Is there some kind of scoping issue?

var currentPlayer = {
  "player_id": "placeholder"
};

$.ajax({
  url: "/players/summary",
  success: function(json) {
    // when this ajax call is completed the placeholder string is not replaced by
    // currentPlayer.player_id.  I have verified that the currentPlayer hash does
    // have that value after this ajax call
    currentPlayer = json;
  }
});

var dashboardViewModel = {
  // <span data-bind="text:currentPlayer"></span> displays the "placeholder"
  // string upon initialization
  currentPlayer: ko.observable(currentPlayer.player_id),
  vsPlayer: ko.observable("VS: ALL PLAYERS")
};

ko.applyBindings(dashboardViewModel);

EDIT:

This is how I solved the issue:

var dashboardViewModel = {
  currentPlayer: ko.observable({"player_id": ""}),
  vsPlayer: ko.observable("VS: ALL PLAYERS")
};

ko.applyBindings(dashboardViewModel);

$.get("/players/summary", dashboardViewModel.currentPlayer);

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

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

发布评论

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

评论(1

要设置可观察值的值,您需要将新值作为第一个参数传递(与初始化它时的广告相同)。

因此,在您的回调中,您可能需要类似的内容:
DashboardViewModel.currentPlayer(json.player_id);

修改原始对象不会更新 currentPlayer 的值。

To set an observable's value, you need to pass the new value in as the first argument (same ad when you initialized it).

So, in your callback, you would want to something like:
dashboardViewModel.currentPlayer(json.player_id);

Modifying your original object would not update currentPlayer's value.

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