Skip to content

Commit

Permalink
Added function execution to client on return message
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Aug 15, 2010
1 parent f1941b7 commit 46fd0e3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
6 changes: 5 additions & 1 deletion static/client.html
Expand Up @@ -11,7 +11,11 @@ <h2 id="pageName">Parent page</h2>
<script>
var client = new ArcClient("http://localhost:8081/static/server.html");

client.sendMessage('test',null,null);
client.sendMessage('test',function(data){

console.log("message sent back from server" + data);

},null);

</script>
</body>
Expand Down
24 changes: 14 additions & 10 deletions static/js/arc_client.js
Expand Up @@ -6,6 +6,7 @@

var self = this;
this.queue = new Array();
this.requests = new Object();
this.iFrame = document.createElement('iframe');
this.iFrame.src = url;
this.frameLoaded = false;
Expand Down Expand Up @@ -35,10 +36,10 @@
};
//: sendMessage
// @options #optional
ArcClient.prototype.sendMessage = function(call, callback, params){
ArcClient.prototype.sendMessage = function(call, callbackF, params){
if(!this.frameLoaded){
console.log("frame not loaded.. Queing " + call);
this.queue.push( { call:call, callback:callback, params:params } );
this.queue.push( { call:call, callback:callbackF, params:params } );
return;
}
if(this.queue.length > 0){
Expand All @@ -47,19 +48,22 @@
this.sendMessage(msgParams.call, msgParams.callback, msgParams.params);
}
console.log("call:"+call+" host:"+this.host);
if(callback){
if(callbackF){
var id = Math.floor(Math.random()*100000+(new Date().getTime()));
window.addEventListener('message', function(e){
e.data.id === id && callback.call(this,e);
}, false);

}
var data = {'call': call,'id':id};
var data = {'call': call,'id':id, callbackId:call + "-" + id };

this.requests[data.callbackId] = callbackF;

if(params) data.params = params;
this.iFrame.contentWindow.postMessage(JSON.stringify(data), this.host);
};
ArcClient.prototype.receiveMessage = function(message){
if (event.origin !== this.host)
return;
ArcClient.prototype.receiveMessage = function(event){
if (event.origin !== this.host) { return; }

this.requests[event.data.callbackId]();

};

window.ArcClient = ArcClient;
Expand Down

0 comments on commit 46fd0e3

Please sign in to comment.