2
내 HTML이 페이지 초기화시 "자리 표시 자"를 표시하지만 내 아약스 호출로 업데이트되지 않습니다. 어떤 종류의 범위 지정 문제가 있습니까?내 녹아웃 관찰 가능 항목이 업데이트되지 않는 이유는 무엇입니까?
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);
UI를 통해 전파되도록 원래 데이터 구조를 수정하는 것만으로 바인딩 작업을 수행 할 필요가 없습니까? 거기에 대시 보드 ViewModel 해시/클래스 내에서 그 연결을 만들 수있는 방법이 있습니까? –
변화에 반응해야하는 것은 관찰 대상을 통과해야합니다. 일반적인 패턴은 원본 데이터를 가져 와서 observables가있는 구조체로 변환 한 다음 서버로 전달하기 전에'ko.toJS' 또는'ko.toJSON'를 사용하여 다시 변환하는 것입니다. –