Author: jmorliaguet
Date: Thu Feb  2 23:03:24 2006
New Revision: 2274

Modified:
   cpsskins/branches/jmo-perspectives/ui/framework/cpsskins.js
   cpsskins/branches/jmo-perspectives/ui/framework/tests/unit/cpsskins_test.html
   
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/cpsskins_storage_adapters.pt
Log:

- the event object now specifies both a subscriber and a target
  (the observed object) otherwise too many events need to be handled and
  it becomes difficult to get access the targetted object.



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 Thu Feb  2 
23:03:24 2006
@@ -81,43 +81,45 @@
   },
 
   /* Internal events */
-  subscribe: function(obj, event) {
-    if (!(event in CPSSkins.Subscribers)) {
-      CPSSkins.Subscribers[event] = $A([]);
+  subscribe: function(eventid, event) {
+    if (!(eventid in CPSSkins.Subscribers)) {
+      CPSSkins.Subscribers[eventid] = $A([]);
     }
-    CPSSkins.Subscribers[event].push(obj);
+    CPSSkins.Subscribers[eventid].push(event);
   },
 
-  unsubscribe: function(obj, event) {
+  unsubscribe: function(eventid, event) {
     new_subscribers = $A([]);
-    CPSSkins.Subscribers[event].each(function(s) {
-      if (obj != s) {
-        new_subscribers.push(s);
+    CPSSkins.Subscribers[eventid].each(function(e) {
+      if (event.subscriber != e.subscriber && event.target != e.target) {
+        new_subscribers.push(e);
       }
     });
-    CPSSkins.Subscribers[event] = new_subscribers;
+    CPSSkins.Subscribers[eventid] = new_subscribers;
   },
 
-  notify: function(context, event) {
-    var subscribers = CPSSkins.Subscribers[event];
-    $A(subscribers).each(function(s) {
-      var handler = CPSSkins.getEventHandler(s, event);
-      if (handler) handler(s, context);
+  notify: function(eventid, target) {
+    var subscribers = CPSSkins.Subscribers[eventid];
+    $A(subscribers).each(function(e) {
+      if (e.target == target) {
+        var handler = CPSSkins.getEventHandler(eventid, e.subscriber);
+        if (handler) handler(e);
+      }
     });
   },
 
-  registerEventHandler: function(subscriber, event, handler) {
+  registerEventHandler: function(eventid, subscriber, handler) {
     var handlers = subscriber._handlers;
     if (!handlers) {
       subscriber._handlers = new Object();
     }
-    subscriber._handlers[event] = handler;
+    subscriber._handlers[eventid] = handler;
   },
 
-  getEventHandler: function(subscriber, event) {
+  getEventHandler: function(eventid, subscriber) {
     var handlers = subscriber._handlers;
     if (handlers) {
-      return handlers[event];
+      return handlers[eventid];
     } else {
       return null;
     }
@@ -635,21 +637,22 @@
   },
 
   addObserver: function(view) {
+    var model = this;
     // register an event handler for the 'modified' event
-    view.registerEventHandler('modified', function(v, context) {
-      var data = v.model.def.data;
-      v.render(data);
+    CPSSkins.registerEventHandler('modified', view, function(event) {
+        var data = event.target.def.data;
+        event.subscriber.render(data);
     });
-    // subscribe to 'modified' events
-    view.subscribeTo('modified');
+    // subscribe to 'modified' events on the model
+    CPSSkins.subscribe('modified', {'subscriber': view, 'target': model});
     // create a back-reference
-    view.model = this;
+    view.model = model;
     // initialize the view
     view.update();
   },
 
   removeObserver: function(view) {
-    view.unsubscribeFrom('modified');
+    CPSSkins.unsubscribe('modified', {'subscriber': view, 'target': model});
   },
 
   _setStorageAdapter: function() {
@@ -749,7 +752,7 @@
   storeData: function(data) {
     /* Store the data directly */
     this.model.def.data = data;
-    CPSSkins.notify(this.model, 'modified');
+    CPSSkins.notify('modified', this.model);
   }
 
 });
@@ -765,7 +768,7 @@
       onComplete: function(req) {
         var data = JSON.parse(req.responseText);
         model.def.data = data;
-        CPSSkins.notify(model, 'modified');
+        CPSSkins.notify('modified', model);
       }
     });
   },
@@ -783,7 +786,7 @@
       onComplete: function(req) {
         var data = JSON.parse(req.responseText);
         model.def.data = data;
-        CPSSkins.notify(model, 'modified');
+        CPSSkins.notify('modified', model);
       }
     });
   }
@@ -828,18 +831,6 @@
 
   /* Private API */
 
-  subscribeTo: function(event) {
-    CPSSkins.subscribe(this, event);
-  },
-
-  registerEventHandler: function(event, handler) {
-    CPSSkins.registerEventHandler(this, event, handler);
-  },
-
-  unsubscribeFrom: function(event) {
-    CPSSkins.unsubscribe(this, event);
-  },
-
   observe: function(model) {
     model.addObserver(this);
     this.model = model;

Modified: 
cpsskins/branches/jmo-perspectives/ui/framework/tests/unit/cpsskins_test.html
==============================================================================
--- 
cpsskins/branches/jmo-perspectives/ui/framework/tests/unit/cpsskins_test.html   
    (original)
+++ 
cpsskins/branches/jmo-perspectives/ui/framework/tests/unit/cpsskins_test.html   
    Thu Feb  2 23:03:24 2006
@@ -382,37 +382,36 @@
 
     testSubscribe: function() { with(this) {
       var info;
-      var handler1 = function(subscriber, context) {
-        info = {'subscriber': subscriber.id, 'context': context.id};
+      var handler1 = function(event) {
+        info = {'subscriber': event.subscriber.id, 'target': event.target.id};
       }
 
       var obj1 = new Object({'id': 'object1'});
       var context1 = new Object({'id': 'context1'});
       var context2 = new Object({'id': 'context2'});
 
-      CPSSkins.registerEventHandler(obj1, 'event1', handler1);
-      CPSSkins.subscribe(obj1, 'event1');
+      CPSSkins.registerEventHandler('event1', obj1, handler1);
+      CPSSkins.subscribe('event1', {'subscriber': obj1, 'target': context1});
 
       assertEqual($H(info).inspect(), $H({}).inspect());
 
       var info = null;
-      CPSSkins.notify(context1, 'event2');
+      CPSSkins.notify('event2', context1);
       assertEqual($H(info).inspect(), $H({}).inspect());
 
       var info = null;
-      CPSSkins.notify(context1, 'event1');
+      CPSSkins.notify('event1', context1);
       assertEqual($H(info).inspect(),
-                  $H({"subscriber":"object1","context":"context1"}).inspect());
+                  $H({"subscriber":"object1","target":"context1"}).inspect());
 
       var info = null;
-      CPSSkins.notify(context2, 'event1');
-      assertEqual($H(info).inspect(),
-                  $H({"subscriber":"object1","context":"context2"}).inspect());
+      CPSSkins.notify('event1', context2);
+      assertEqual($H(info).inspect(), $H({}).inspect());
 
-      CPSSkins.unsubscribe(obj1, 'event1');
+      CPSSkins.unsubscribe('event1', {'subscriber': obj1, 'target': context1});
 
       var info = null;
-      CPSSkins.notify(context1, 'event1');
+      CPSSkins.notify('event1', context1);
       assertEqual($H(info).inspect(), $H({}).inspect());
 
     }},

Modified: 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/cpsskins_storage_adapters.pt
==============================================================================
--- 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/cpsskins_storage_adapters.pt
    (original)
+++ 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/cpsskins_storage_adapters.pt
    Thu Feb  2 23:03:24 2006
@@ -224,7 +224,8 @@
   </div>
 
   <h2>Compound storage</h2>
-  <p>A compound storage is created by combining the storages of different 
models.</p>
+  <p>A compound storage is created by combining the storages of different
+     models.</p>
 
   <div id="area3">
     <ins class="model">
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to