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 = {}));
-
\$\begingroup\$ But, is this javascript? In javascript there's no "static" as in Java or C# and there are no concurrency issues since javascript is single thread. \$\endgroup\$ema– ema2014年08月01日 05:54:46 +00:00Commented Aug 1, 2014 at 5:54
1 Answer 1
After some testing I discovered that HomeActions._ajaxSettings
is indeed static and would cause issues if simultaneous calls were made to the API. After some time away from the code the obvious solution popped into my head as shown below. I've been too caught up in learning the confusing syntax of javascript that the basics of OOP design fell to the wayside just so I could get a working prototype.
I added a "class" called Action
and moved the Start()
method to it and in each static method, RefreshCache
and IndexAllRecords
, I now return a new Action
class which maintains its state separately with each call to the API.
var MVC;
(function (MVC) {
var Action = (function () {
function Action(ajs) {
this.ajs = ajs;
}
Action.prototype.Start = function () {
return $.ajax(this.ajs);
};
return Action;
}) ();
var HomeActions = (function () {
function HomeActions() {}
HomeActions.RefreshCache = function (key, returnUrl, ajaxSettings) {
ajaxSettings = ajaxSettings || {};
ajaxSettings['data'] = JSON.stringify({
key: key,
returnUrl: returnUrl
});
ajaxSettings['url'] = MVC.Home.ActionNames.RefreshCache;
if ('type' in ajaxSettings == false) ajaxSettings['type'] = 'POST';
if ('context' in ajaxSettings == false) ajaxSettings['context'] = this;
if ('contentType' in ajaxSettings == false) ajaxSettings['contentType'] = 'application/json';
return new Action(ajaxSettings);
};
HomeActions.IndexAllRecords = function (ajaxSettings) {
ajaxSettings = ajaxSettings || {};
ajaxSettings['data'] = JSON.stringify({
});
ajaxSettings['url'] = MVC.Home.ActionNames.IndexAllRecords;
if ('type' in ajaxSettings == false) ajaxSettings['type'] = 'POST';
if ('context' in ajaxSettings == false) ajaxSettings['context'] = this;
if ('contentType' in ajaxSettings == false) ajaxSettings['contentType'] = 'application/json';
return new Action(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 = {}));