summaryrefslogtreecommitdiff
path: root/addons/base/static/src/js/controller.js
diff options
context:
space:
mode:
authorAntony Lesuisse <al@openerp.com>2011-07-15 16:21:12 +0200
committerAntony Lesuisse <al@openerp.com>2011-07-15 16:21:12 +0200
commitf0defe0af75e1b789656b748221b6dbc79c89fcb (patch)
tree5304cafe42c02e7ca3f7037fe60d746336f85e8d /addons/base/static/src/js/controller.js
parent2decff7b213725caaa372fbde01a2f1d22aca7bf (diff)
controller move callback and basiccontroller to controller
bzr revid: al@openerp.com-20110715142112-vginop8ms0bgxje5
Diffstat (limited to 'addons/base/static/src/js/controller.js')
-rw-r--r--addons/base/static/src/js/controller.js133
1 files changed, 132 insertions, 1 deletions
diff --git a/addons/base/static/src/js/controller.js b/addons/base/static/src/js/controller.js
index 0d2fe0a9cc0..aca7c5fe004 100644
--- a/addons/base/static/src/js/controller.js
+++ b/addons/base/static/src/js/controller.js
@@ -71,7 +71,138 @@ openerp.base.controller = function(instance) {
// todo change john resig class to keep window clean
instance.base.Class = window.Class
-// Controller will move here
+instance.base.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;
+ }
+ r = c.callback.apply(c.self, c.args.concat(args));
+ // TODO special value to stop the chain
+ // openerp.base.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"
+ });
+ };
+
+ return callback.add({
+ callback: method,
+ self:obj,
+ args:Array.prototype.slice.call(arguments, 2)
+ });
+};
+
+/**
+ * OpenERP Controller
+ */
+instance.base.BasicController = instance.base.Class.extend( /** @lends instance.base.BasicController# */{
+ /**
+ * rpc operations, event binding and callback calling should be done in
+ * start() instead of init so that event can be hooked in between.
+ *
+ * @constructs
+ */
+ init: function(element_id) {
+ this.element_id = element_id;
+ this.$element = $('#' + element_id);
+ if (element_id) {
+ instance.screen[element_id] = this;
+ }
+
+ // Transform on_* method into openerp.base.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] = instance.base.callback(this, this[name]);
+ }
+ }
+ }
+ },
+ /**
+ * Controller start
+ * event binding, rpc and callback calling required to initialize the
+ * object can happen here
+ *
+ * Returns a promise object letting callers (subclasses and direct callers)
+ * know when this component is done starting
+ *
+ * @returns {jQuery.Deferred}
+ */
+ start: function() {
+ // returns an already fulfilled promise. Maybe we could return nothing?
+ // $.when can take non-deferred and in that case it simply considers
+ // them all as fulfilled promises.
+ // But in thise case we *have* to ensure callers use $.when and don't
+ // try to call deferred methods on this return value.
+ return $.Deferred().done().promise();
+ },
+ stop: function() {
+ },
+ log: function() {
+ var args = Array.prototype.slice.call(arguments);
+ var caller = arguments.callee.caller;
+ // TODO add support for line number using
+ // https://github.com/emwendelin/javascript-stacktrace/blob/master/stacktrace.js
+ // args.unshift("" + caller.debug_name);
+ this.on_log.apply(this,args);
+ },
+ on_log: function() {
+ if(window.openerp.debug || (window.location.search.indexOf('?debug') !== -1)) {
+ var notify = false;
+ var body = false;
+ if(window.console) {
+ console.log(arguments);
+ } else {
+ body = true;
+ }
+ var a = Array.prototype.slice.call(arguments, 0);
+ for(var i = 0; i < a.length; i++) {
+ var v = a[i]==null ? "null" : a[i].toString();
+ if(i==0) {
+ notify = v.match(/^not/);
+ body = v.match(/^bod/);
+ }
+ if(body) {
+ $('<pre></pre>').text(v).appendTo($('body'));
+ }
+ if(notify && this.notification) {
+ this.notification.notify("Logging:",v);
+ }
+ }
+ }
+
+ }
+});
};