What is a Namespace
Namespace is a container for set of identifiers, functions, methods and all that. It gives a level of direction to its contents so that it will be well distinguished and organized.
var yourNamespace = {
foo: function() {
},
bar: function() {
}
};
...
yourNamespace.foo();
You can see the below example in Detail-
//AJAX Synchronous and Asynchronous Requests.
//#REGION NAMESPACE
var demo = demo || {};
//#ENDREGION
demo.ajax = demo.ajax || (function () {
var getBaseURL = function () {
var currentBaseURL = location.protocol + "//" + location.hostname +
(location.port && ":" + location.port) + "/";
return currentBaseURL;
};
var baseURL = getBaseURL();
var request_token;
var ajaxAsyncCall = function (requestURL, typeGP, inputs, request_token) {
//Ajax Async CALL
$.ajax({
url: requestURL,
type: typeGP,
contentType: "application/json; charset=utf-8",
data: inputs,
beforeSend: function (xhr) {
xhr.setRequestHeader("Request_Token", request_token)
},
async: true,
cache: false,
success: function (data) {
if (data !== undefined && data !== null) {
if (data.Code == "OK") {
alert("Success");
return false;
}
else if (data.Code == "ERROR") {
alert('Error - ' + data.Message);
return false;
}
}
}
});
};
var ajaxSyncCall = function (requestURL, typeGP, inputs, request_token) {
//Ajax Sync CALL
$.ajax({
url: requestURL,
type: true,
contentType: "application/json; charset=utf-8",
data: inputs,
beforeSend: function (xhr) {
xhr.setRequestHeader("Request_Token", request_token)
},
async: false,
cache: false,
success: function (data) {
if (data !== undefined && data !== null) {
if (data.Code == "OK") {
alert("Success");
return false;
}
else if (data.Code == "ERROR") {
alert('Error - ' + data.Message);
return false;
}
}
}
});
};
return {
baseURL:baseURL,
ajaxAsyncCall: ajaxAsyncCall,
ajaxSyncCall: ajaxSyncCall
}
})();
Use of CREATED Synchronous and Asynchronous common methods in other JavaScript file.
//Use of common Synchronous and Asynchronous methods.
//GET CUSTOMERS LIST WITH SYNC CALL.
demo.ajax.ajaxSyncCall(demo.ajax.baseURL + "API/Users/GetUsersRequest", 'GET', null, function(data) {
if (data != undefined) {
successGetCustomers(data);
}
}, null);
//GET CUSTOMERS LIST WITH ASYNC CALL.
demo.ajax.ajaxAsyncCall(demo.ajax.baseURL + "API/Users/GetUsersRequest", 'GET', null, function(data) {
if (data != undefined) {
successGetCustomers(data);
}
}, null);
//Success Method
var successGetCustomers = function (data) {
if (data) {
console.log(data);
//TODO: As PER YOU!
}
};
0 comments:
Post a Comment