Skip to content

Commit

Permalink
Massive update
Browse files Browse the repository at this point in the history
Removed a lot of functions relating to Core.Address from the sandbox.
Address should not be dealt with by the core and I will phase it out
over time. Added the ability to make sure all requests received by
Core.Ajax have a header attached (useful in detecting 300 redirects)
Core.Communication now returns an array of responses from every
notification sent. Core.Databinding (which relies on knockout.js and
therefore should probably be removed) now doesn't attempt to remove
bindings if the element is not found in the DOM. Core.ModuleGrouping now
can raise events on the shutdown of module groups. Core.Storage now
tests if local storage is full (via a try/catch) and clears local
storage if it is. Added a helper class for building URL's. (should be in
another place, need associated utilities)
  • Loading branch information
aranm committed Oct 7, 2013
1 parent 4796b71 commit dd24439
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 81 deletions.
4 changes: 1 addition & 3 deletions Core.Address.js
Expand Up @@ -258,9 +258,7 @@
mappings = [];
currentParameters = {};
},
navigateToUrl: function (url) {
return navigateToUrl(url);
},
navigateToUrl: navigateToUrl,
removeAddressComponent: function (parameter) {
if (currentParameters[parameter] !== undefined) {
currentParameters[parameter] = "";
Expand Down
68 changes: 60 additions & 8 deletions Core.Ajax.js
Expand Up @@ -8,6 +8,12 @@
return new Date();
}
},

hasHeaderResponseValues = false,

expectedHeaderResponseValues = [],

missingHeadersCallback = null,

tryCallFunction = function (callback, context, argument) {
if (typeof callback === "function") {
Expand All @@ -21,9 +27,23 @@
},

callSuccess = function (moduleId, tempRequest, returnValue) {
if (Core.moduleIsActive(moduleId)) {
if (returnValue === null) {
tryCallFunction(tempRequest.success, tempRequest.context, null);
if (Core.moduleIsActive(moduleId) || Core.Singleton.singletonIsActive(moduleId)) {
if (returnValue === null || returnValue === undefined) {
tryCallFunction(tempRequest.success, tempRequest.context, returnValue);
}
else if ((typeof returnValue == 'string' || returnValue instanceof String)) {
//if we have received something that looks like xml (naive starts with a '<')
if (returnValue.substring(0, 1) === "<" && tempRequest.acceptsXmlResponse === true) {
tryCallFunction(tempRequest.success, tempRequest.context, returnValue);
}
//if we can have a string response
else if (tempRequest.acceptsStringResponse == true) {
tryCallFunction(tempRequest.success, tempRequest.context, returnValue);
}
//otherwise it is a failure
else {
tryCallFunction(tempRequest.failure, tempRequest.context, "Server returned an unexpected response, this may indicate that you are no longer logged in to the system. Please refresh your browser");
}
}
else if (returnValue.RequestSucceeded === undefined) {
tryCallFunction(tempRequest.success, tempRequest.context, returnValue);
Expand All @@ -41,7 +61,7 @@
},

callFailure = function (moduleId, tempRequest, returnValue) {
if (Core.moduleIsActive(moduleId)) {
if (Core.moduleIsActive(moduleId) || Core.Singleton.singletonIsActive(moduleId)) {
tryCallFunction(tempRequest.failure, tempRequest.context, returnValue);
}
},
Expand Down Expand Up @@ -410,7 +430,26 @@
responseFunction(moduleId, requestData, returnValue);
}
},
success = function (returnValue) {
success = function(returnValue, status, associatedData) {
var wasSuccessful = true,
i,
arrayLength,
headerResponse;

if (hasHeaderResponseValues == true && associatedData != undefined && associatedData.getResponseHeader != undefined) {
arrayLength = expectedHeaderResponseValues.length;
for (i = 0; i < arrayLength && wasSuccessful === true; i++) {
headerResponse = expectedHeaderResponseValues[i];
if (associatedData.getResponseHeader(headerResponse.key) != headerResponse.value) {
wasSuccessful = false;
}
}
}

if (wasSuccessful === false && missingHeadersCallback != null) {
missingHeadersCallback(returnValue, status, associatedData);
}

storedRequests.removeRequest(currentAjaxRequest, moduleId);
callFunction(callSuccess, returnValue);
},
Expand All @@ -420,18 +459,22 @@
},
errorFunc = function (jqXhr, textStatus, errorThrown) {
storedRequests.removeRequest(currentAjaxRequest, moduleId);
if (textStatus === "error") {
// We might need a proper message if no error message passed in
callFunction(callFailure, errorThrown !== "" ? errorThrown : "Failure to connect to the server");
}
//when a page navigate occurs ajax requests are cancelled, both status and ready state are 0
if (jqXhr.status === 0 && jqXhr.readyState === 0) { }
else if (jqXhr.status === 0 && jqXhr.readyState === 0) { }
//when we abort an ajax call we dont want to call the failure method
else if (textStatus === "abort") { }
else {
callFunction(callFailure, errorThrown);
}
removeAllQueuedRequests();
},
successAndCache = function (ajaxReturnValue) {
successAndCache = function (ajaxReturnValue, status, associatedData) {
cache.addDataToCache(ajaxReturnValue, urlMapping, requestData);
success(ajaxReturnValue);
success(ajaxReturnValue, status, associatedData);
};

if (requestData === null || requestData === undefined) {
Expand Down Expand Up @@ -492,6 +535,15 @@

cancelRequests: function (moduleId) {
storedRequests.abortAllForModule(moduleId);
},

expectedResponseHeaderValues: function (headerRepsonseValues) {
expectedHeaderResponseValues = headerRepsonseValues;
hasHeaderResponseValues = headerRepsonseValues != null && headerRepsonseValues.length > 0;
},

reponseFailedDueToMissingHeadersCallback: function (callback){
missingHeadersCallback = callback;
}
};
};
Expand Down
31 changes: 22 additions & 9 deletions Core.Communication.js
Expand Up @@ -20,26 +20,39 @@
notify: function (topic) {

if (!handlers[topic]) {
return true;
return undefined;
}

var args = slice.call(arguments, 1),
type = topic,
ret = true,
i,
len,
msgList;
msgList,
msg,
returnValue,
returnValues = [];

if (handlers[type] instanceof Array) {
msgList = handlers[type];
for (i = 0, len = msgList.length; i < len && ret === true; i++) {
ret = msgList[i].callback.apply(msgList[i].context, args);
if (ret === undefined) {
ret = true;
len = msgList.length;
for (i = 0; i < len; i++) {
msg = msgList[i];
returnValue = msg.callback.apply(msg.context, args);
if (returnValue !== undefined) {
returnValues.push(returnValue);
}
}
}
return ret;

if (returnValues.length === 0) {
return undefined;
}
else if (returnValues.length === 1) {
return returnValues[0];
}
else {
return returnValues;
}
},

removeListener: function (topic, callbackFunction) {
Expand Down Expand Up @@ -79,7 +92,7 @@
}
};
};

// Expose Core as an AMD module
if (typeof define === "function" && define.amd) {
define("Core.Communication", ["Core"], function (core) {
Expand Down
15 changes: 7 additions & 8 deletions Core.DataBinding.js
Expand Up @@ -54,21 +54,20 @@
throw new Error("No mapping defined for: " + moduleId);
}


for (i = 0, arrayLength = mappings.length; i < arrayLength; i++) {
domElement = mappings[i];

if (domElement === undefined || domElement === null) {
errors += "Unmapped moduleId passed to bind: " + moduleId + '\n';
}

element = dom.getElementById(domElement);
if (element === undefined || element === null) {
errors += "Undefined dom element passed to bind: " + domElement + '\n';
}
else {
//remove any bindings on the node
ko.cleanNode(element);
element = dom.getElementById(domElement);
//if the dom element no longer exists on the screen, we don't need to unbind it
if (element === undefined || element === null) { }
else {
//remove any bindings on the node
ko.cleanNode(element);
}
}
}

Expand Down
26 changes: 13 additions & 13 deletions Core.DomManipulation.js
Expand Up @@ -49,24 +49,24 @@
getDom: function() {
return domManipulation;
},
openDialog: function(name, closedCallback) {
var closedFunction,
element = $(elementMappings[name]);

closedFunction = function () {
//unbind the 'hide' event
element.unbind('hidden', closedFunction);

openDialog: function (name, closedCallback, openCallback) {
var closedFunction = function () {
if (closedCallback !== undefined && typeof closedCallback === "function") {
closedCallback();
}
},
openFunction = function () {
if (openCallback !== undefined && typeof openCallback === "function") {
openCallback();
}
};


//attach the shown and hidden events to only fire once
$(elementMappings[name]).one('shown', openFunction);
$(elementMappings[name]).one('hidden', closedFunction);

//show the dialog
element.modal('show');

//attach an event to the "hide"
element.bind('hidden', closedFunction);
$(elementMappings[name]).modal('show');
},
closeDialog: function(name) {
var element = $(elementMappings[name]);
Expand Down
13 changes: 13 additions & 0 deletions Core.ModuleGrouping.js
Expand Up @@ -7,6 +7,7 @@
previewEvents = {},
restartEvents = {},
postEvents = {},
shutDownEvents = {},
slice = [].slice,
inArray = function (list, item) {
var i, arrayLength, found = false;
Expand Down Expand Up @@ -135,6 +136,9 @@
if (foundIndex !== -1) {
runningModuleGroupings.splice(foundIndex, 1);
}

raiseEvents(groupingName, shutDownEvents);
//communication.notify("ModuleGroupingStopping", groupingName);
}
return returnValue;
},
Expand Down Expand Up @@ -262,6 +266,15 @@
}
postEvents[groupingName].push(args);
},
registerShutDownEvents: function () {
var groupingName = arguments[0],
args = slice.call(arguments, 1);

if (!shutDownEvents[groupingName]) {
shutDownEvents[groupingName] = [];
}
shutDownEvents[groupingName].push(args);
},
start: function (groupingName) {
return startModuleGroup(groupingName);
},
Expand Down
2 changes: 1 addition & 1 deletion Core.Navigation.js
@@ -1,4 +1,4 @@
/*globals Core*/
/*globals Core, define, require*/

(function () {
var coreNavigation = function (communication, moduleGrouping) {
Expand Down
6 changes: 5 additions & 1 deletion Core.Singleton.js
Expand Up @@ -7,11 +7,15 @@
},
getSingleton = function (singletonId) {
return singletons[singletonId];
},
singletonIsActive = function(singletonId) {
return getSingleton(singletonId) !== undefined;
};

return {
registerSingleton: registerSingleton,
getSingleton: getSingleton
getSingleton: getSingleton,
singletonIsActive: singletonIsActive
};
};

Expand Down

0 comments on commit dd24439

Please sign in to comment.