为什么我的淘汰赛观察结果没有更新?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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.