I apologize if this is a duplicated question somewhere but I could not find an answer that works.
I have an .aspx page that is calling a webservice which populates basic employee information. It works great when I call the service outside of a custom object. As soon as I move the ajax functionality into the custom object, I lose scope and cannot populate the object properties. I can see that the service is returning data in the json via Firebug but the object's properties do not get populated. Any help on this is greatly appreciated. The custom object JavaScript is below.
/* Begin Employee JavaScript object */
//Define the Employee object
function Employee() {}
//Define the Employee object's properties on the prototype
Employee.prototype = {
//Properties
EmployeeNumber: '',
FirstName: '',
LastName: '',
ErrorMessage: '',
IsFound: false
}
//Define the Search function
Employee.prototype.employeeSearch = function (pEmplNo, callback) {
$.ajax({
context: this,
type: "POST",
url: "webservice URL",
data: '{"pEmployeeNumber":"' + pEmplNo + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var me = this;
me.EmployeeNumber = msg.d.EmployeeNumber;
me.FirstName = msg.d.FirstName;
me.LastName = msg.d.LastName;
me.ErrorMessage = '';
me.IsFound = true;
callback(me);
},
error: function () {
var me = this;
me.ErrorMessage = 'Error retrieving employee';
me.IsFound = false;
}
});
}
/* End Employee JavaScript object */
function SetEmployeeFields(pEmployee) {
$('#<%= txtEmployeeNumber.ClientID %>').val(pEmployee.EmployeeNumber);
$('#<%= txtFirstName.ClientID %>').val(pEmployee.FirstName);
$('#<%= txtLastName.ClientID %>').val(pEmployee.LastName);
}
function FindEmployee(emplNo) {
$("#mdlGetEmployeeDialog").dialog('open');
if (emplNo.length < 6) {
while (emplNo.length < 6) {
emplNo = "0" + emplNo;
}
$('#<%= txtEmployeeNumber.ClientID %>').val(emplNo);
}
var _employee = new Employee();
_employee.employeeSearch(emplNo, function () {
if (_employee.IsFound) {
SetEmployeeFields(_employee);
$('#<%= txtNature.ClientID %>').focus();
$("#mdlGetEmployeeDialog").dialog('close');
}
else {
$('#<%= lblMessage.ClientID %>').text('Could not find employee' + _employee.ErrorMessage);
$('#<%= upErrors.ClientID %>').addClass("ui-state-error").css({ "padding": "4px", "margin-top": "4px" }).show();
$("#mdlGetEmployeeDialog").dialog('close');
}
});
}
//Sets up the Employee Search modal and button
function SetupGetEmployeeModal() {
$("#mdlGetEmployeeDialog").dialog({
autoOpen: false,
modal: true,
width: 500
});
$("#btnEmployeeSearch").button({
text: false,
icons: {
primary: "ui-icon-search"
}
}).css({
'padding': '4px 0px 4px 0px',
'font-size': '9px',
'margin-left': '3px'
});
}
$(function () {
$('#btnEmployeeSearch').click(function (e) {
e.preventDefault();
var emplNo = $('#<%= txtEmployeeNumber.ClientID %>').val();
if (emplNo !== '' && emplNo !== '######') {
$('#<%= lblMessage.ClientID %>').text('');
$('#<%= upErrors.ClientID %>').removeClass('ui-state-error').removeAttr('style').hide();
FindEmployee(emplNo);
}
else {
$('#<%= lblMessage.ClientID %>').text('Employee Number required');
$('#<%= upErrors.ClientID %>').addClass("ui-state-error").css({ "padding": "4px", "margin-top": "4px" }).show();
$("#mdlGetEmployeeDialog").dialog('close');
}
});
});
/* End Page Scripts */