cleanup Message code

This commit is contained in:
rolux 2013-02-24 15:06:08 +05:30
parent 9c46e17b9f
commit c1f9e9d7a3
3 changed files with 23 additions and 22 deletions

View file

@ -296,8 +296,8 @@ Ox.load.UI = function(options, callback) {
var callback; var callback;
if (Ox.isObject(arguments[0])) { if (Ox.isObject(arguments[0])) {
Ox.forEach(arguments[0], function(callback, event) { Ox.forEach(arguments[0], function(callback, event) {
Ox.Message.bind(function(evt, data, oxid) { Ox.Message.bind(function(event_, data, oxid) {
if (Ox.isUndefined(oxid) && event == evt) { if (event_ == event && Ox.isUndefined(oxid)) {
callback(data); callback(data);
} }
}); });

View file

@ -93,12 +93,12 @@ Ox.Element = function(options, self) {
that.on('load', function() { that.on('load', function() {
Ox.Message.post(that, 'init', {id: that.oxid}); Ox.Message.post(that, 'init', {id: that.oxid});
}); });
that.onMessage = function(callback) { that.onMessage = function() {
var callback; var callback;
if (Ox.isObject(arguments[0])) { if (Ox.isObject(arguments[0])) {
Ox.forEach(function(callback, event) { Ox.forEach(arguments[0], function(callback, event) {
Ox.Message.bind(arguments[0], function(evt, data, oxid) { Ox.Message.bind(arguments[0], function(event_, data, oxid) {
if (that.oxid ==oxid && event == evt) { if (event_ == event && oxid == that.oxid) {
callback(data); callback(data);
} }
}); });

View file

@ -1,14 +1,9 @@
Ox.Message = (function() { /*@
/* Ox.Message <o> Message controller
{ @*/
oxid: if coming from an iframe
event: string Ox.Message = (function() {
data: object
}
Ox.Message.post('play', {foo:'bar'});
Ox.Message.post(iframe, {event: 'play', data: {foo:'bar'}});
*/
var that = {}, var that = {},
callbacks = []; callbacks = [];
@ -24,22 +19,26 @@ Ox.Message = (function() {
callback(data.event, data.data, data.oxid); callback(data.event, data.data, data.oxid);
}); });
} else { } else {
Ox.Log('Message', 'unknown message', e.data); Ox.Log('Core', 'unknown message', e.data);
} }
}); });
that.bind = function(callback) { that.bind = function(callback) {
callbacks.push(callback); callbacks.push(callback);
} };
that.post = function(/* [target], event, data */) {
that.post = function(/*[target, ]event, data */) {
var target, data, event; var target, data, event;
if (arguments.length == 2) { if (arguments.length == 2) {
target = window.parent; target = window.parent;
event = arguments[0]; event = arguments[0];
data = arguments[1]; data = arguments[1];
} else { } else {
target = Ox.isElement(arguments[0][0]) ? arguments[0][0] : $('#' + arguments[0])[0]; target = (
target = target.contentWindow; Ox.isElement(arguments[0][0])
? arguments[0][0]
: $('#' + arguments[0])[0]
).contentWindow;
event = arguments[1]; event = arguments[1];
data = arguments[2]; data = arguments[2];
} }
@ -48,6 +47,8 @@ Ox.Message = (function() {
data: data, data: data,
oxid: window.oxid oxid: window.oxid
}), '*'); }), '*');
} };
return that; return that;
})(); })();