2011-12-17 5 views
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); 

답변

3

당신이 (첫 번째 인수로 새로운 가치를 전달하는 동일한 광고 할 때 필요의 관측 값을 설정하려면 :

이것은 내가 문제를 해결하는 방법입니다 당신이 그것을 초기화했다).

그래서 콜백에서 다음과 같이하고 싶을 것입니다 : dashboardViewModel.currentPlayer (json.player_id);

원본 개체를 수정해도 currentPlayer의 값은 업데이트되지 않습니다.

+1

UI를 통해 전파되도록 원래 데이터 구조를 수정하는 것만으로 바인딩 작업을 수행 할 필요가 없습니까? 거기에 대시 보드 ViewModel 해시/클래스 내에서 그 연결을 만들 수있는 방법이 있습니까? –

+0

변화에 반응해야하는 것은 관찰 대상을 통과해야합니다. 일반적인 패턴은 원본 데이터를 가져 와서 observables가있는 구조체로 변환 한 다음 서버로 전달하기 전에'ko.toJS' 또는'ko.toJSON'를 사용하여 다시 변환하는 것입니다. –

관련 문제