add filter to wbo to set their id automatically based on uri
authorDan Mills <thunder@mozilla.com>
Thu, 20 Nov 2008 09:16:08 +0900
changeset 45039 595856d1d19080921c11242ecc7e6506b738c42a
parent 45038 c21ebe6983698b18dda0ad3cee6c86638c3acc90
child 45040 23a02fb0477af30090f79be4abb7a953d85fe590
push idunknown
push userunknown
push dateunknown
add filter to wbo to set their id automatically based on uri
services/sync/modules/base_records/wbo.js
--- a/services/sync/modules/base_records/wbo.js
+++ b/services/sync/modules/base_records/wbo.js
@@ -51,27 +51,30 @@ function WBORecord(uri, authenticator) {
   this._WBORec_init(uri, authenticator);
 }
 WBORecord.prototype = {
   __proto__: Resource.prototype,
   _logName: "Record.WBO",
 
   _WBORec_init: function WBORec_init(uri, authenticator) {
     this._init(uri, authenticator);
+    this.pushFilter(new WBOFilter());
     this.pushFilter(new JsonFilter());
     this.data = {
-      id: "",
-      modified: "2454725.98283", // FIXME
+      // modified: "2454725.98283", // FIXME
       payload: {}
     };
   },
 
-  get id() this.data.id,
-  set id(value) {
-    this.data.id = value;
+  // id is special because it's based on the uri
+  get id() {
+    if (this.data.id)
+      return decodeURI(this.data.id);
+    let foo = this.uri.spec.split('/');
+    return decodeURI(foo[foo.length-1]);
   },
 
   get parentid() this.data.parentid,
   set parentid(value) {
     this.data.parentid = value;
   },
 
   get modified() this.data.modified,
@@ -87,8 +90,24 @@ WBORecord.prototype = {
   get payload() this.data.payload,
   set payload(value) {
     this.data.payload = value;
   }
 
   // note: encryption is part of the CryptoWrapper object, which uses
   // a string (encrypted) payload instead of an object
 };
+
+function WBOFilter() {}
+WBOFilter.prototype = {
+  beforePUT: function(data, wbo) {
+    let self = yield;
+    let foo = wbo.uri.spec.split('/');
+    data.id = decodeURI(foo[foo.length-1]);
+    self.done(data);
+  },
+  afterGET: function(data, wbo) {
+    let self = yield;
+    let foo = wbo.uri.spec.split('/');
+    data.id = decodeURI(foo[foo.length-1]);
+    self.done(data);
+  }
+};