2012-09-26 4 views
2

사용자 모델의 관찰 가능한 배열을 가진 뷰 모델 설정이 있습니다. 항목 추가/제거가 올바르게 작동하지만 항목을 어떻게 업데이트합니까? 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/

+0

'date_active'와'date_inactive'도 observables로 만들려고 했습니까? KO는 그들에 대한 접근을 탐지하고 관련 관찰 자료를 구독해야합니다. – lanzz

+0

저는 그것에 대해 생각해 보았습니다. 그러나 저는 KO에 새로운 사람인 것처럼 그 일을하는 방법을 정확히 모르겠습니다. – Aaron

답변

2

에서 적응하지 데이터 그 자체이다. @Ianzz가 지정한대로 date-activedate_inactive 관측치를 만들어야합니다. 그 후 당신의 HTML에서

function User(username, date_inactive, date_active) { 
    this.username = username; 
    this.date_active = ko.observable(date_active); 
    this.date_inactive = ko.observable(date_inactive); 
}; 

, 전체 예를 들어

<div data-bind="foreach: Users"> 
    <input data-bind="value: date_active"/> 
    <input data-bind="value: date_inactive"/> 
<div>​ 

같은 것을 참조 fiddle을한다.

+0

이것이 내가 놓친 다른 부분이라고 생각합니다. 값을 직접 설정하는 대신 함수를 호출해야합니다. this.date_active (date_active); 날짜를 설정하십시오. – Aaron

+0

@ Rynan - 고마워! 나는 똑같은 문제에 직면했다. 처음 몇 단어는 "관측 가능한 배열은 배열에 대한 변경 내용 만 추적한다"는 진술이 모든 것을 정리했다는 것이다. – Faisal

관련 문제