観察可能なユーザーオブジェクトの配列を使用してビューモデルを設定しています。アイテムの追加/削除は正しく機能していますが、アイテムを更新するにはどうすればよいですか?koindexOf関数を使用して値を見つけることができます。
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 );
}
}
});
}
viewUserModel.usersの値は、サーバーのjson応答から配列にプッシュされます。
ユーザーがボタンをクリックし、サーバーが正常に応答したときに、date_activeとdate_inactiveの値を更新できるようにしたいと思います。
私のセットアップはhttp://net.tutsplus.com/tutorials/javascript-ajax/into-the-ring-with-knockout-js-the-title-fight/からの適応です