I'm working on a client-side API for my ASP.net MVC application and I'm not quite sure I've got the code right. This API allows me to call server-side action methods via Ajax and I've designed it to have a similar call syntax like jQuery uses.
Is the variable HomeActions._ajaxSettings static the way this written right now? I'm concerned that if two or more calls are made simultaneously to this API that they would effectively be using/updating the same static variable and cause intermittent bugs or failure. If this variable is an issue how can it be re-written and still maintain the call pattern shown in the example?
Example usage:
MVC.Home.Actions.RefreshCache("myKey").Start().success(function (result) {
// refresh the window
window.location.reload(true);
}).error(function () {
console.error("epic fail");
});
API code:
var MVC;
(function (MVC) {
var HomeActions = (function () {
function HomeActions() {}
HomeActions.RefreshCache = function (key, returnUrl, ajaxSettings) {
HomeActions._ajaxSettings = ajaxSettings || {};
HomeActions._ajaxSettings['data'] = JSON.stringify({
key: key,
returnUrl: returnUrl
});
HomeActions._ajaxSettings['url'] = MVC.Home.ActionNames.RefreshCache;
if ('type' in HomeActions._ajaxSettings == false) HomeActions._ajaxSettings['type'] = 'POST';
if ('context' in HomeActions._ajaxSettings == false) HomeActions._ajaxSettings['context'] = this;
if ('contentType' in HomeActions._ajaxSettings == false) HomeActions._ajaxSettings['contentType'] = 'application/json';
return this;
};
HomeActions.IndexAllRecords = function (ajaxSettings) {
HomeActions._ajaxSettings = ajaxSettings || {};
HomeActions._ajaxSettings['data'] = JSON.stringify({
});
HomeActions._ajaxSettings['url'] = MVC.Home.ActionNames.IndexAllRecords;
if ('type' in HomeActions._ajaxSettings == false) HomeActions._ajaxSettings['type'] = 'POST';
if ('context' in HomeActions._ajaxSettings == false) HomeActions._ajaxSettings['context'] = this;
if ('contentType' in HomeActions._ajaxSettings == false) HomeActions._ajaxSettings['contentType'] = 'application/json';
return this;
};
HomeActions.Start = function () {
return $.ajax(HomeActions._ajaxSettings);
};
return HomeActions;
}) ();
var HomeActionNames = (function () {
function HomeActionNames() {}
HomeActionNames.RefreshCache = '/refreshcache';
HomeActionNames.IndexAllRecords = '/indexallrecords';
return HomeActionNames;
}) ();
var Home = (function () {
function Home() {}
Home.Actions = HomeActions;
Home.ActionNames = HomeActionNames;
return Home;
}) ();
MVC.Home = Home;
}) (MVC || (MVC = {}));