Commit c3739581 authored by Sven Franck's avatar Sven Franck

added routerjs stateless deferred and requestService callback function

parent 56325b61
...@@ -50,6 +50,10 @@ ...@@ -50,6 +50,10 @@
// validate URL: // validate URL:
// http://bit.ly/2Ol4gj // http://bit.ly/2Ol4gj
// deferred explanation:
// http://bit.ly/WH2TRI
// http://bit.ly/zm0Csi
/*jslint indent: 2, maxlen: 80, nomen: true */ /*jslint indent: 2, maxlen: 80, nomen: true */
/*global window: true, $: true, undefined: true, console: true, /*global window: true, $: true, undefined: true, console: true,
document: true, require: true*/ document: true, require: true*/
...@@ -60,6 +64,39 @@ ...@@ -60,6 +64,39 @@
// ================== utility methods ================== // ================== utility methods ==================
// extend $.deferred to allow multiple calls and resolves
// thx router.js
$.extend({
StatelessDeferred: function () {
var doneList = $.Callbacks("memory"),
promise = {
done: doneList.add,
// Get a promise for this deferred
// If obj is provided, the promise aspect is added to the object
promise: function (obj) {
var i,
keys = ['done', 'promise'];
if (obj === undefined) {
obj = promise;
} else {
for (i = 0; i < keys.length; i += 1) {
obj[keys[i]] = promise[keys[i]];
}
}
return obj;
}
},
deferred = promise.promise({});
deferred.resolveWith = doneList.fireWith;
deferred.resolve = doneList.fire;
// All done!
return deferred;
}
});
// => cross-browser reduce (no support in ie8-, opera 12-) // => cross-browser reduce (no support in ie8-, opera 12-)
// http://mzl.la/11tnDy1 // http://mzl.la/11tnDy1
if ('function' !== typeof Array.prototype.reduce) { if ('function' !== typeof Array.prototype.reduce) {
...@@ -279,6 +316,18 @@ ...@@ -279,6 +316,18 @@
// ================== internal methods ================== // ================== internal methods ==================
// => keep service callbacks available until a postMessage returns response
priv.trackCallback = function (id, callback, callbackFunction) {
if (priv.callbackTracker === undefined) {
priv.callbackTracker = [];
}
priv.callbackTracker.push({
"id": id,
"callback": callback,
"callbackFunction": callbackFunction
});
};
// => keep track of service requesters to reply to // => keep track of service requesters to reply to
priv.trackRequest = function (id, respondTo) { priv.trackRequest = function (id, respondTo) {
if (priv.serviceTracker === undefined) { if (priv.serviceTracker === undefined) {
...@@ -287,6 +336,17 @@ ...@@ -287,6 +336,17 @@
priv.serviceTracker.push({"id": id, "respondTo": respondTo}); priv.serviceTracker.push({"id": id, "respondTo": respondTo});
}; };
// => retrieve callback for a specific service call
priv.retrieveCallback = function (id) {
var i, callback;
for (i = 0; i < priv.callbackTracker.length; i += 1) {
callback = priv.callbackTracker[i];
if (callback.id = id) {
return [callback.callback, callback.callbackFunction];
}
}
};
// => retrieve window which called a specific service // => retrieve window which called a specific service
priv.retrieveCallingWindow = function (id) { priv.retrieveCallingWindow = function (id) {
var i, service, callee; var i, service, callee;
...@@ -456,6 +516,7 @@ ...@@ -456,6 +516,7 @@
break; break;
case "request": case "request":
trackingId = priv.generateUuid(); trackingId = priv.generateUuid();
// track this request, so we know where to send the response // track this request, so we know where to send the response
priv.trackRequest(trackingId, event.originalTarget); priv.trackRequest(trackingId, event.originalTarget);
// request the service // request the service
...@@ -473,23 +534,25 @@ ...@@ -473,23 +534,25 @@
priv.sendServiceReply(event); priv.sendServiceReply(event);
break; break;
case "reply": case "reply":
priv.returnResult(event.data.result); priv.returnResult(event);
break; break;
} }
}; };
// => return the result to the function call // => return the result to the function call
priv.returnResult = function (result) { priv.returnResult = function (event) {
// need a way to return this result to the calling function var callback = priv.retrieveCallback(event.data.callback);
console.log(result); // resolve the deferred, which includes the requestService callback
return result; callback[0].resolve(event.data.result, callback[1]);
}; };
// => sends a response message after a service has been run // => sends a response message after a service has been run
priv.sendServiceReply = function (event) { priv.sendServiceReply = function (event) {
var targetWindow = priv.retrieveCallingWindow(event.data.trackingId); var targetWindow = priv.retrieveCallingWindow(event.data.trackingId);
targetWindow.postMessage({ targetWindow.postMessage({
"type": "reply", "type": "reply",
"result": event.data.result "result": event.data.result,
"callback": event.data.callbackId,
}, window.location.href.split("?")[0]); }, window.location.href.split("?")[0]);
}; };
...@@ -499,7 +562,8 @@ ...@@ -499,7 +562,8 @@
window.top.postMessage({ window.top.postMessage({
"type": "result", "type": "result",
"result": result, "result": result,
"trackingId" : event.data.trackingId "trackingId" : event.data.trackingId,
"callbackId": event.data.callbackId,
}, window.location.href.split("?")[0]); }, window.location.href.split("?")[0]);
}; };
...@@ -535,10 +599,12 @@ ...@@ -535,10 +599,12 @@
return tgt && tgt.getElementById(o).contentWindow; return tgt && tgt.getElementById(o).contentWindow;
}, document); }, document);
} }
// and request the service // and request the service
targetWindow.postMessage({ targetWindow.postMessage({
"type": "run", "type": "run",
"trackingId": trackingId, "trackingId": trackingId,
"callbackId": event.data.callbackId,
"service": event.data.service, "service": event.data.service,
"parameters": event.data.parameters "parameters": event.data.parameters
}, window.location.href); }, window.location.href);
...@@ -826,11 +892,25 @@ ...@@ -826,11 +892,25 @@
}; };
// => request a service to be run // => request a service to be run
that.requestService = $.fn.requestService = function (options) { that.requestService = $.fn.requestService = function (options, callbackFunction) {
var deferred = new $.StatelessDeferred(),
callbackId = priv.generateUuid(),
callback = deferred;
// store callback to be retrieved by response handler
priv.trackCallback(callbackId, callback, callbackFunction);
// set type // set type
if (options.type === undefined) { if (options.type === undefined) {
options.type = "request/any"; options.type = "request/any";
} }
deferred.done(function(result, callbackFunction) {
if (callbackFunction) {
callbackFunction(result);
}
});
options.callbackId = callbackId;
window.top.postMessage(options, window.location.href); window.top.postMessage(options, window.location.href);
}; };
...@@ -887,10 +967,8 @@ ...@@ -887,10 +967,8 @@
success: function (data) { success: function (data) {
priv.appendGadget(data, options); priv.appendGadget(data, options);
}, },
error: function (error, status, message) { error: function (error) {
console.log(error); console.log(error);
console.log(status);
console.log(message);
} }
}); });
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment