Antony Lesuisse (OpenERP) has proposed merging 
lp:~openerp-dev/openerp-web/trunk-event-refactoring-al into lp:openerp-web.

Requested reviews:
  OpenERP R&D Web Team (openerp-dev-web)

For more details, see:
https://code.launchpad.net/~openerp-dev/openerp-web/trunk-event-refactoring-al/+merge/91377

Event refactoring, replace CallbackEnabled by a mostly backward compatible 
backbone style event mechanism.
-- 
https://code.launchpad.net/~openerp-dev/openerp-web/trunk-event-refactoring-al/+merge/91377
Your team OpenERP R&D Team is subscribed to branch 
lp:~openerp-dev/openerp-web/trunk-event-refactoring-al.
=== modified file 'addons/web/static/src/js/chrome.js'
--- addons/web/static/src/js/chrome.js	2012-02-01 15:44:42 +0000
+++ addons/web/static/src/js/chrome.js	2012-02-03 00:28:18 +0000
@@ -140,7 +140,7 @@
     }
 });
 
-openerp.web.CrashManager = openerp.web.CallbackEnabled.extend({
+openerp.web.CrashManager = openerp.web.EventEnabled.extend({
     init: function() {
         this._super();
         openerp.connection.on_rpc_error.add(this.on_rpc_error);
@@ -1234,6 +1234,8 @@
 
 }
 
+
+
 };
 
 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax:

=== modified file 'addons/web/static/src/js/core.js'
--- addons/web/static/src/js/core.js	2012-01-25 08:53:10 +0000
+++ addons/web/static/src/js/core.js	2012-02-03 00:28:18 +0000
@@ -115,65 +115,6 @@
     };
 })();
 
-openerp.web.callback = function(obj, method) {
-    var callback = function() {
-        var args = Array.prototype.slice.call(arguments);
-        var r;
-        for(var i = 0; i < callback.callback_chain.length; i++)  {
-            var c = callback.callback_chain[i];
-            if(c.unique) {
-                callback.callback_chain.splice(i, 1);
-                i -= 1;
-            }
-            var result = c.callback.apply(c.self, c.args.concat(args));
-            if (c.callback === method) {
-                // return the result of the original method
-                r = result;
-            }
-            // TODO special value to stop the chain
-            // openerp.web.callback_stop
-        }
-        return r;
-    };
-    callback.callback_chain = [];
-    callback.add = function(f) {
-        if(typeof(f) == 'function') {
-            f = { callback: f, args: Array.prototype.slice.call(arguments, 1) };
-        }
-        f.self = f.self || null;
-        f.args = f.args || [];
-        f.unique = !!f.unique;
-        if(f.position == 'last') {
-            callback.callback_chain.push(f);
-        } else {
-            callback.callback_chain.unshift(f);
-        }
-        return callback;
-    };
-    callback.add_first = function(f) {
-        return callback.add.apply(null,arguments);
-    };
-    callback.add_last = function(f) {
-        return callback.add({
-            callback: f,
-            args: Array.prototype.slice.call(arguments, 1),
-            position: "last"
-        });
-    };
-    callback.remove = function(f) {
-        callback.callback_chain = _.difference(callback.callback_chain, _.filter(callback.callback_chain, function(el) {
-            return el.callback === f;
-        }));
-        return callback;
-    };
-
-    return callback.add({
-        callback: method,
-        self:obj,
-        args:Array.prototype.slice.call(arguments, 2)
-    });
-};
-
 /**
  * Generates an inherited class that replaces all the methods by null methods (methods
  * that does nothing and always return undefined).
@@ -332,24 +273,116 @@
     }
 });
 
-openerp.web.CallbackEnabled = openerp.web.Class.extend(/** @lends openerp.web.CallbackEnabled# */{
+openerp.web.EventEnabled = openerp.web.Class.extend({
     /**
      * @constructs openerp.web.CallbackEnabled
      * @extends openerp.web.Class
      */
     init: function() {
+        var self = this;
+        var callback = function(obj, name, method) {
+            var callback = function() {
+                var args = Array.prototype.slice.call(arguments);
+                return self.trigger.apply(self, [name].concat(args));
+            };
+            callback.add = function(f) {
+                if(typeof(f) == 'function') {
+                    f = { callback: f, args: Array.prototype.slice.call(arguments, 1) };
+                }
+                f.self = f.self || null;
+                f.args = f.args || [];
+                f.unique = !!f.unique;
+                if(f.position == 'last') {
+                    self.on(name, f.callback, obj);
+                } else {
+                    self.on(name, f.callback, obj);
+                }
+                return callback;
+            };
+            callback.add_first = function(f) {
+                return callback.add.apply(null,arguments);
+            };
+            callback.add_last = function(f) {
+                return callback.add({
+                    callback: f,
+                    args: Array.prototype.slice.call(arguments, 1),
+                    position: "last"
+                });
+            };
+            callback.remove = function(f) {
+                self.off(name, f);
+                return callback;
+            };
+            return callback.add({
+                callback: method,
+                self:obj,
+                args:Array.prototype.slice.call(arguments, 3)
+            });
+        };
         // Transform on_* method into openerp.web.callbacks
         for (var name in this) {
             if(typeof(this[name]) == "function") {
                 this[name].debug_name = name;
                 // bind ALL function to this not only on_and _do ?
-                if((/^on_|^do_/).test(name)) {
-                    this[name] = openerp.web.callback(this, this[name]);
+                if((/^do_/).test(name)) {
+                    this[name] = _.bind(this[name], this);
+                } else if((/^on_/).test(name)) {
+                    this[name] = callback(this, name, this[name]);
                 }
             }
         }
     },
     /**
+     * @param {String} event event to listen to on the current object, null for all events
+     * @param {Function} handler event handler to bind to the relevant event
+     * @returns this
+     */
+    on: function (event, handler) {
+
+        if(handler == undefined)
+            debugger;
+        var calls = this['_callbacks'] || (this._callbacks = {});
+
+        if (event in calls) {
+            calls[event].push(handler);
+        } else {
+            calls[event] = [handler];
+        }
+        return this;
+    },
+    /**
+     * @param {String} event event to unbind on the current object
+     * @param {function} [handler] specific event handler to remove (otherwise unbind all handlers for the event)
+     * @returns this
+     */
+    off: function (event, handler) {
+        var calls = this._callbacks || {};
+        if (!(event in calls)) { return this; }
+        if (!handler) {
+            delete calls[event];
+        } else {
+            var handlers = calls[event];
+            handlers.splice(
+                _(handlers).indexOf(handler),
+                1);
+        }
+        return this;
+    },
+    /**
+     * @param {String} event
+     * @returns this
+     */
+    trigger: function (event) {
+        var args = Array.prototype.slice.call(arguments, 1);
+        var calls;
+        if (!(calls = this._callbacks)) { return this; }
+        var callbacks = (calls[event] || []).concat(calls[null] || []);
+        for(var i=0, length=callbacks.length; i<length; ++i) {
+            callbacks[i].apply(this, args);
+        }
+        return this;
+    },
+    /**
      * Proxies a method of the object, in order to keep the right ``this`` on
      * method invocations.
      *
@@ -376,7 +409,7 @@
     }
 });
 
-openerp.web.Connection = openerp.web.CallbackEnabled.extend( /** @lends openerp.web.Connection# */{
+openerp.web.Connection = openerp.web.EventEnabled.extend( /** @lends openerp.web.Connection# */{
     /**
      * @constructs openerp.web.Connection
      * @extends openerp.web.CallbackEnabled
@@ -934,7 +967,7 @@
  *
  * That will kill the widget in a clean way and erase its content from the dom.
  */
-openerp.web.Widget = openerp.web.CallbackEnabled.extend(/** @lends openerp.web.Widget# */{
+openerp.web.Widget = openerp.web.EventEnabled.extend(/** @lends openerp.web.Widget# */{
     /**
      * The name of the QWeb template that will be used for rendering. Must be
      * redefined in subclasses or the default render() method can not be used.

=== modified file 'addons/web/static/src/js/data.js'
--- addons/web/static/src/js/data.js	2012-01-30 09:53:39 +0000
+++ addons/web/static/src/js/data.js	2012-02-03 00:28:18 +0000
@@ -806,7 +806,7 @@
     on_unlink: function(ids) {}
 });
 
-openerp.web.Model = openerp.web.CallbackEnabled.extend({
+openerp.web.Model = openerp.web.EventEnabled.extend({
     init: function(model_name) {
         this._super();
         this.model_name = model_name;

_______________________________________________
Mailing list: https://launchpad.net/~openerp-dev-gtk
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~openerp-dev-gtk
More help   : https://help.launchpad.net/ListHelp

Reply via email to