사용자 모델의 관찰 가능한 배열을 가진 뷰 모델 설정이 있습니다. 항목 추가/제거가 올바르게 작동하지만 항목을 어떻게 업데이트합니까? ko indexOf 함수를 사용하여 값을 찾을 수 있습니다. viewUserModel.users의knockoutjs observable 배열 항목을 업데이트하는 방법
function User(username, date_inactive, date_active) {
this.username = username;
this.date_active = date_active;
this.date_inactive = date_inactive;
};
User.prototype.inactivateMe = function() {
json_responses.push(this);
$.getJSON("url" + this.username, function(json) {
original = json_response.pop();
//do update here
});
};
userModel = [ ], //Where the loaded usernames are stored.
viewUserModel = {
users: ko.observableArray(userModel)
//.......
//This is how I'm adding users to the array.
addUser: function() {
$.getJSON("url",
{ username: usern },
function(json) {
if(json.STATUS != undefined && json.STATUS == 'success') {
newuser = new User(json.USERNAME, json.DATE_ACTIVE, json.DATE_INACTIVE );
viewUserModel.users.push(newuser);
}
}
});
}
값은 서버의 JSON reponse의 배열로 가압된다.
사용자가 버튼을 클릭하고 서버가 성공으로 응답 할 때 date_active 및 date_inactive 값을 업데이트 할 수 있기를 원합니다.
내 설치는 관찰 어레이만을 (예컨대 푸시 및 팝 등) 배열에 대한 변경을 추적 http://net.tutsplus.com/tutorials/javascript-ajax/into-the-ring-with-knockout-js-the-title-fight/
'date_active'와'date_inactive'도 observables로 만들려고 했습니까? KO는 그들에 대한 접근을 탐지하고 관련 관찰 자료를 구독해야합니다. – lanzz
저는 그것에 대해 생각해 보았습니다. 그러나 저는 KO에 새로운 사람인 것처럼 그 일을하는 방법을 정확히 모르겠습니다. – Aaron