2
\$\begingroup\$

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 = {}));
Quill
12k5 gold badges41 silver badges93 bronze badges
asked Jul 29, 2014 at 18:58
\$\endgroup\$
1
  • \$\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\$ Commented Aug 1, 2014 at 5:54

1 Answer 1

1
\$\begingroup\$

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 = {}));
answered Aug 1, 2014 at 4:16
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.