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;
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);
}
});

View file

@ -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);
}
});

View file

@ -1,14 +1,9 @@
Ox.Message = (function() {
/*
{
oxid: if coming from an iframe
event: string
data: object
}
/*@
Ox.Message <o> 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;
})();