フルアップデート(更新チェックあり)
クライアントから送信されたキーと値のペアで、サーバー上のデータを完全に上書きする方法です。サーバーにあった値は、上書きによって失われます。
更新の際、「楽観的ロック(Optimistic lock)」の機能によるチェックを行います。クライアントへの KiiObject の取得以降に、サーバー上で他のクライアントからの更新があった場合は、更新処理がエラー応答します。
更新のコード例を以下に挙げます。
-
// Instantiate a KiiObject. var object2 = KiiObject.objectWithURI(object.objectURI()); // Refresh the KiiObject to get the latest key-value pairs. object2.refresh().then( function(theObject) { // Update key-value pairs. theObject.set("myid", 1); theObject.set("name", "John Doe Jr"); theObject.set("email", "john_jr@example.com"); theObject.remove("address"); // Save and fully update the KiiObject. // This method removes all key-value pairs from the KiiObject on the server and // adds the key-value pairs generated locally to the KiiObject. return theObject.saveAllFields(null, false); } ).then( function(theObject) { // Do something. } ).catch( function(error) { var theObject = error.target; var errorString = error.message; // Handle the error. } );
-
// Instantiate a KiiObject. var object2 = KiiObject.objectWithURI(object.objectURI()); // Refresh the KiiObject to get the latest key-value pairs. object2.refresh({ success: function(theObject) { // Update key-value pairs. theObject.set("myid", 1); theObject.set("name", "John Doe Jr"); theObject.set("email", "john_jr@example.com"); theObject.remove("address"); // Save and fully update the KiiObject. // This method removes all key-value pairs from the KiiObject on the server and // adds the key-value pairs generated locally to the KiiObject. theObject.saveAllFields({ success: function(theObject) { // Do something. }, failure: function(theObject, errorString) { // Handle the error. } }, false); }, failure: function(theObject, errorString) { // Handle the error. } });
ここでは以下の処理を行っています。
- 更新対象の KiiObject を用意します。ここでは、URI からの生成によって、既存の KiiObject を作成しています。コード上の URI は、事前に取得しておいたものに置き換えてください。
refresh()
メソッドによってサーバーの KiiObject を取得します。更新チェックを行う場合は KiiObject の更新状態を示す_version
値が必要なため、取得処理は必須です。set()
メソッドでキーと値のペアの追加を、remove()
メソッドでキーと値のペアの削除を行います。更新後はここでobject
に設定されている値だけになります。saveAllFields()
メソッドの第 2 引数を false にして更新します。サーバー側の KiiObject が他のクライアントによって書き換えられていた場合は、API がエラー応答します。