From c1f9e9d7a388b3571ef76b9203481fe0aa896a0d Mon Sep 17 00:00:00 2001 From: rolux Date: Sun, 24 Feb 2013 15:06:08 +0530 Subject: [PATCH] cleanup Message code --- source/Ox.UI/Ox.UI.js | 4 ++-- source/Ox.UI/js/Core/Element.js | 8 ++++---- source/Ox.UI/js/Core/Message.js | 33 +++++++++++++++++---------------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/source/Ox.UI/Ox.UI.js b/source/Ox.UI/Ox.UI.js index 3b25968f..ba7f79bc 100644 --- a/source/Ox.UI/Ox.UI.js +++ b/source/Ox.UI/Ox.UI.js @@ -296,8 +296,8 @@ Ox.load.UI = function(options, callback) { var callback; if (Ox.isObject(arguments[0])) { Ox.forEach(arguments[0], function(callback, event) { - Ox.Message.bind(function(evt, data, oxid) { - if (Ox.isUndefined(oxid) && event == evt) { + Ox.Message.bind(function(event_, data, oxid) { + if (event_ == event && Ox.isUndefined(oxid)) { callback(data); } }); diff --git a/source/Ox.UI/js/Core/Element.js b/source/Ox.UI/js/Core/Element.js index ae859d21..1f67588e 100644 --- a/source/Ox.UI/js/Core/Element.js +++ b/source/Ox.UI/js/Core/Element.js @@ -93,12 +93,12 @@ Ox.Element = function(options, self) { that.on('load', function() { Ox.Message.post(that, 'init', {id: that.oxid}); }); - that.onMessage = function(callback) { + that.onMessage = function() { var callback; if (Ox.isObject(arguments[0])) { - Ox.forEach(function(callback, event) { - Ox.Message.bind(arguments[0], function(evt, data, oxid) { - if (that.oxid ==oxid && event == evt) { + Ox.forEach(arguments[0], function(callback, event) { + Ox.Message.bind(arguments[0], function(event_, data, oxid) { + if (event_ == event && oxid == that.oxid) { callback(data); } }); diff --git a/source/Ox.UI/js/Core/Message.js b/source/Ox.UI/js/Core/Message.js index 96b22ce8..b9e94533 100644 --- a/source/Ox.UI/js/Core/Message.js +++ b/source/Ox.UI/js/Core/Message.js @@ -1,14 +1,9 @@ -Ox.Message = (function() { - /* - { - oxid: if coming from an iframe - event: string - data: object - } +/*@ +Ox.Message Message controller +@*/ + +Ox.Message = (function() { - Ox.Message.post('play', {foo:'bar'}); - Ox.Message.post(iframe, {event: 'play', data: {foo:'bar'}}); - */ var that = {}, callbacks = []; @@ -24,22 +19,26 @@ Ox.Message = (function() { callback(data.event, data.data, data.oxid); }); } else { - Ox.Log('Message', 'unknown message', e.data); + Ox.Log('Core', 'unknown message', e.data); } }); that.bind = function(callback) { callbacks.push(callback); - } - that.post = function(/* [target], event, data */) { + }; + + that.post = function(/*[target, ]event, data */) { var target, data, event; if (arguments.length == 2) { target = window.parent; event = arguments[0]; data = arguments[1]; } else { - target = Ox.isElement(arguments[0][0]) ? arguments[0][0] : $('#' + arguments[0])[0]; - target = target.contentWindow; + target = ( + Ox.isElement(arguments[0][0]) + ? arguments[0][0] + : $('#' + arguments[0])[0] + ).contentWindow; event = arguments[1]; data = arguments[2]; } @@ -48,6 +47,8 @@ Ox.Message = (function() { data: data, oxid: window.oxid }), '*'); - } + }; + return that; + })();