2

観察可能なユーザーオブジェクトの配列を使用してビューモデルを設定しています。アイテムの追加/削除は正しく機能していますが、アイテムを更新するにはどうすればよいですか?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/からの適応です

4

1 に答える 1

2

監視可能な配列は、データ自体ではなく、配列に加えられた変更(プッシュやポップなど)のみを追跡します。@Ianzzが指定したようdate-activeに、オブザーバブルを作成する必要があります。date_inactive

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

その後、HTMLで、次のようなことを行います。

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

完全な例については、フィドルを参照してください。

于 2012-09-26T21:18:43.103 に答える