Author: jmorliaguet
Date: Sun Jan 29 15:01:01 2006
New Revision: 2237

Modified:
   cpsskins/branches/jmo-perspectives/ui/framework/cpsskins.js
   
cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_storage_adapters.html
Log:

- storage adapters: began implementing data requesters

- observers are now dictionaries (not lists)



Modified: cpsskins/branches/jmo-perspectives/ui/framework/cpsskins.js
==============================================================================
--- cpsskins/branches/jmo-perspectives/ui/framework/cpsskins.js (original)
+++ cpsskins/branches/jmo-perspectives/ui/framework/cpsskins.js Sun Jan 29 
15:01:01 2006
@@ -16,6 +16,23 @@
 
 */
 
+// For debugging - to remove later.
+function debug(obj) {
+  var msg, debugbox;
+  debugbox = $("message");
+  if (!debugbox) {
+    var div = document.createElement("DIV");
+    div.setAttribute("id", "message");
+    document.getElementsByTagName("body").item(0).appendChild(div);
+  }
+  try {
+     msg = obj.inspect().escapeHTML();
+  } catch(e) {
+     msg = e;
+  }
+  $("message").innerHTML += msg + '<br/>';
+}
+
 var CPSSkins = {
   Version: "0.5",
 
@@ -90,15 +107,15 @@
   load: function(el, classid, index) {
 
       // make the element identifiable
-      var el_id = classid + index;
-      el.setAttribute("id", el_id);
+      var name = classid + index;
+      el.setAttribute("name", name);
 
       var def = CPSSkins.jsonParse(el);
 
       switch(classid) {
 
         case "model": {
-          CPSSkins.Models[el_id] = new CPSSkins.Model(el, def);
+          CPSSkins.Models[name] = new CPSSkins.Model(el, def);
           break;
         }
 
@@ -302,11 +319,11 @@
 Object.extend(Canvas, {
 
   getModel: function(node) {
-    if (!node) return;
+    if (!node) return null;
     var model_node = this.getModelNode(node);
     if (model_node) {
-      var id = model_node.getAttribute("id");
-      return CPSSkins.Models[id];
+      var name = model_node.getAttribute("name");
+      return CPSSkins.Models[name];
     }
     return null;
   },
@@ -509,39 +526,40 @@
   initialize: function(node, def) {
     this.node = this.node;
     this.def = def;
-
     // observers
-    this.observers = $A([]);
-
+    this.observers = $H({});
     // set the storage adapter
     this._setStorageAdapter();
   },
 
   getData: function() {
-    return this.storage.getData()
+    this.storage.requestData(); /* asynchronous call */
+    return this.def.data;
   },
 
   setData: function(data) {
-    this.storage.setData(data);
+    this.storage.storeData(data);
 
     // Update the observers
     var observers = this.observers;
     if (observers) {
       observers.each(function(o) {
-        o.update();
+        o[1].update();
       });
     }
-
   },
 
+  // TODO: to rewrite: this might not work if called asynchronously.
   updateData: function(data) {
-    var current = this.getData() || new Object();
-    Object.extend(data, current);
+    Object.extend(data, this.data || new Object());
     this.setData(data);
   },
 
   addObserver: function(view) {
-    this.observers.push(view);
+    var view_id = view.id;
+    if (!(view_id in this.observers)) {
+      this.observers[view_id] = view;
+    }
     // create a back-reference
     view.model = this;
     // initialize the view
@@ -549,13 +567,13 @@
   },
 
   removeObserver: function(view) {
-    this.observers = this.observers.without(view);
+    //this.observers = this.observers.without(view);
   },
 
   _setStorageAdapter: function() {
     var storage_def = this.def.storage;
     if (!storage_def) {
-      storage_def = {"type": "canvas"};
+      storage_def = {"type": "client"};
     }
     this.storage = Storages[storage_def.type](this);
   }
@@ -571,20 +589,21 @@
   },
 
   /* Public API */
-  getData: function() {
+  requestData: function() {
     /* to be overriden */
   },
 
-  setData: function(data) {
+  storeData: function(data) {
     /* to be overriden */
   }
+
 }
 
 if (!window.Storages) { var Storages = new Object(); }
 Object.extend(Storages, {
 
-  canvas: function(model) {
-    return new CPSSkins.CanvasStorage(model);
+  client: function(model) {
+    return new CPSSkins.ClientStorage(model);
   },
 
   remote: function(model) {
@@ -593,15 +612,16 @@
 
 });
 
-CPSSkins.CanvasStorage = Class.create();
-CPSSkins.CanvasStorage.prototype = Object.extend(
+CPSSkins.ClientStorage = Class.create();
+CPSSkins.ClientStorage.prototype = Object.extend(
   new CPSSkins.StorageAdapter(), {
 
-  getData: function() {
-    return this.model.def.data;
+  requestData: function() {
+    /* nothing to do since the data is already there */
   },
 
-  setData: function(data) {
+  storeData: function(data) {
+    /* Store the data directly */
     this.model.def.data = data;
   }
 
@@ -611,28 +631,26 @@
 CPSSkins.RemoteStorage.prototype = Object.extend(
   new CPSSkins.StorageAdapter(), {
 
-  getData: function() {
-    var url = this.model.def.accessors.get;
-
+  requestData: function() {
+    var url = this.model.def.storage.accessors.get;
+    var model = this.model;
     options = {
       onComplete: function(req) {
         var data = JSON.parse(req.responseText);
-        $("message").innerHTML += data;
-        this.model.data = data;
+        model.def.data = data;
+        $H(observers).each(function(m) {
+          m[1].refresh();
+        });
       }
     }
     new Ajax.Request(url, options);
-    return null;
   },
 
-  setData: function(data) {
-    $("message").innerHTML += def;
-    /* to implement */
+  storeData: function(data) {
   }
 
 });
 
-
 // View
 
 CPSSkins.View = function() {};
@@ -710,12 +728,11 @@
     if (this.model) {
       return this.model.getData();
     }
-    return null;
   },
 
   update: function() {
     var data = this.getData();
-    if (data) this.render(data);
+    if (data != null) this.render(data);
   },
 
   show: function() {
@@ -1161,7 +1178,6 @@
       }
 
       var a = Canvas.createNode(options);
-
       var icon = disabled ? "noicon.png": (item.icon || "noicon.png");
       a.appendChild(Canvas.createNode({
         tag: "img",
@@ -1223,7 +1239,7 @@
     if (!model) return;
     var data = model.def.data;
     if (!data) return;
-    if (!data.hint) return;
+    if (data.hint == null) return;
 
     this.mouseX = Event.pointerX(e);
     this.mouseY = Event.pointerY(e);

Modified: 
cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_storage_adapters.html
==============================================================================
--- 
cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_storage_adapters.html
     (original)
+++ 
cpsskins/branches/jmo-perspectives/ui/framework/tests/functional/cpsskins_storage_adapters.html
     Sun Jan 29 15:01:01 2006
@@ -80,7 +80,5 @@
 
   </div>
 
-  <div id="message"></div>
-
 </body>
 </html>
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to