init
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
define([
|
||||
'jquery'
|
||||
], function ($) {
|
||||
'use strict';
|
||||
|
||||
return function (customerData) {
|
||||
var initialized = false,
|
||||
initPromise,
|
||||
guardedMethods = [
|
||||
'reload',
|
||||
'set',
|
||||
'invalidate',
|
||||
'onAjaxComplete'
|
||||
],
|
||||
emptyArrayMethods = [
|
||||
'getExpiredSectionNames',
|
||||
'getExpiredKeys'
|
||||
];
|
||||
|
||||
if (!customerData || customerData._esunCustomerDataInitGuard) {
|
||||
return customerData;
|
||||
}
|
||||
|
||||
customerData._esunCustomerDataInitGuard = true;
|
||||
initPromise = typeof customerData.getInitCustomerData === 'function' ?
|
||||
customerData.getInitCustomerData() :
|
||||
null;
|
||||
|
||||
if (initPromise && typeof initPromise.done === 'function') {
|
||||
initPromise.done(function () {
|
||||
initialized = true;
|
||||
});
|
||||
} else {
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
function resolveDeferred(deferred, result) {
|
||||
if (result && typeof result.done === 'function') {
|
||||
result.done(function () {
|
||||
deferred.resolve.apply(deferred, arguments);
|
||||
});
|
||||
|
||||
if (typeof result.fail === 'function') {
|
||||
result.fail(function () {
|
||||
deferred.reject.apply(deferred, arguments);
|
||||
});
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
deferred.resolve(result);
|
||||
}
|
||||
|
||||
function runAfterInit(callback) {
|
||||
var deferred = $.Deferred();
|
||||
|
||||
if (initialized) {
|
||||
resolveDeferred(deferred, callback());
|
||||
|
||||
return deferred.promise();
|
||||
}
|
||||
|
||||
if (!initPromise || typeof initPromise.done !== 'function') {
|
||||
initialized = true;
|
||||
resolveDeferred(deferred, callback());
|
||||
|
||||
return deferred.promise();
|
||||
}
|
||||
|
||||
initPromise.done(function () {
|
||||
initialized = true;
|
||||
resolveDeferred(deferred, callback());
|
||||
});
|
||||
|
||||
if (typeof initPromise.fail === 'function') {
|
||||
initPromise.fail(function () {
|
||||
deferred.reject.apply(deferred, arguments);
|
||||
});
|
||||
}
|
||||
|
||||
return deferred.promise();
|
||||
}
|
||||
|
||||
guardedMethods.forEach(function (methodName) {
|
||||
var originalMethod = customerData[methodName];
|
||||
|
||||
if (typeof originalMethod !== 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
customerData[methodName] = function () {
|
||||
var context = this,
|
||||
args = arguments;
|
||||
|
||||
if (initialized) {
|
||||
return originalMethod.apply(context, args);
|
||||
}
|
||||
|
||||
return runAfterInit(function () {
|
||||
return originalMethod.apply(context, args);
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
emptyArrayMethods.forEach(function (methodName) {
|
||||
var originalMethod = customerData[methodName];
|
||||
|
||||
if (typeof originalMethod !== 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
customerData[methodName] = function () {
|
||||
if (!initialized) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return originalMethod.apply(this, arguments);
|
||||
};
|
||||
});
|
||||
|
||||
if (typeof customerData.needReload === 'function') {
|
||||
customerData.needReload = function (originalMethod) {
|
||||
return function () {
|
||||
if (!initialized) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return originalMethod.apply(this, arguments);
|
||||
};
|
||||
}(customerData.needReload);
|
||||
}
|
||||
|
||||
return customerData;
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user