Мой 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);
EDIT:
Это, как я решил эту проблему:
var dashboardViewModel = {
currentPlayer: ko.observable({"player_id": ""}),
vsPlayer: ko.observable("VS: ALL PLAYERS")
};
ko.applyBindings(dashboardViewModel);
$.get("/players/summary", dashboardViewModel.currentPlayer);
Не является ли привязкой, что все, что вам нужно сделать, это изменить исходную структуру данных, чтобы она распространялась через пользовательский интерфейс? Есть ли способ сделать это соединение внутри хэш-класса dashboardViewModel? –
Все, что должно реагировать на изменения, должно проходить через наблюдаемые. Обычный шаблон - взять исходные данные, преобразовать их в структуру с наблюдаемыми, а затем преобразовать обратно с помощью 'ko.toJS' или' ko.toJSON' перед передачей его на сервер. –