I have a javascript object that I instantiate called vacationDayRequested.
function vacationDayRequested(_requestedDate,_clockNo) {
this.status = "unknowm";
this.ClockNo = _clockNo;
this.DayRequested = _requestedDate;
this.Selectable = IsSelectableBASIC(this);
}
I want to assign the value of the Selectable property via an ajax function called IsSelectableBASIC:
function IsSelectableBASIC(vacationDayRequested)
{
var returnValue = false;
$.ajax({
url: "VacationRequest/IsDaySelectableBasic",
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(vacationDayRequested),
success: function (data) {
if (data.OperationMessage == "yes") {
returnValue = true;
}
else {
returnValue = false;
}
}
});
return returnValue;
}
I checked using the developer tools in Chrome that my ajax is returning JSON with an OperationMessage value of "yes" and yet my vacationDayRequested.Selectable comes back as "false". Do I have a design flaw in my javascript ?
Below is how I instantiate the object and use it in my client code:
var vdr = new vacationDayRequested(requestedDate, clockNo);
alert(vdr.Selectable);
UPDATE: 9/11/2014
Callbacks are becoming more clear to me now. Do I have it correct when describing what is happening in the code below ? Also, please let me know if I am using the term callback correctly throughout.
- The button is pressed
- The button click event calls the GetValueOfX function and passes it a callback function named DoSomething.
GetValueOfX executes and has an ajax function where success calls DoSomething because we passed in DoSomething.
Press
$("#btn").click(function () {GetValueOfX(DoSomething); }); function DoSomething(result) { ***//This is the correct place for code that depends on the ajax return*** x = result; debugger; alert(x); } function GetValueOfX(callback) { $.ajax({ url: "/JSTesting/GetSomeTestData", type: 'POST', contentType: 'application/json; charset=utf-8', success: callback }) ***//if we had code here it might fire before the ajax request is complete*** }