Fixed promises on Internet Explorer
This commit is contained in:
parent
ee9fde5402
commit
840039958a
12 changed files with 112 additions and 77 deletions
|
@ -28,6 +28,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script type="text/javascript" src="/js/DI.js"></script>
|
<script type="text/javascript" src="/js/DI.js"></script>
|
||||||
|
<script type="text/javascript" src="/js/Promise.js"></script>
|
||||||
<script type="text/javascript" src="/js/State.js"></script>
|
<script type="text/javascript" src="/js/State.js"></script>
|
||||||
<script type="text/javascript" src="/js/Api.js"></script>
|
<script type="text/javascript" src="/js/Api.js"></script>
|
||||||
<script type="text/javascript" src="/js/Auth.js"></script>
|
<script type="text/javascript" src="/js/Auth.js"></script>
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
var App = App || {};
|
var App = App || {};
|
||||||
|
|
||||||
App.API = function() {
|
App.API = function(promise) {
|
||||||
|
|
||||||
var baseUrl = '/api/';
|
var baseUrl = '/api/';
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ App.API = function() {
|
||||||
var fullUrl = baseUrl + '/' + url;
|
var fullUrl = baseUrl + '/' + url;
|
||||||
fullUrl = fullUrl.replace(/\/{2,}/, '/');
|
fullUrl = fullUrl.replace(/\/{2,}/, '/');
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
return promise.make(function(resolve, reject) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
success: function(data, textStatus, xhr) {
|
success: function(data, textStatus, xhr) {
|
||||||
resolve({
|
resolve({
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
var App = App || {};
|
var App = App || {};
|
||||||
|
|
||||||
App.Auth = function(jQuery, api, appState) {
|
App.Auth = function(jQuery, util, api, appState, promise) {
|
||||||
|
|
||||||
function loginFromCredentials(userName, password, remember) {
|
function loginFromCredentials(userName, password, remember) {
|
||||||
return new Promise(function(resolve, reject) {
|
return promise.make(function(resolve, reject) {
|
||||||
api.post('/login', {userName: userName, password: password})
|
promise.wait(api.post('/login', {userName: userName, password: password}))
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
updateAppState(response);
|
updateAppState(response);
|
||||||
jQuery.cookie(
|
jQuery.cookie(
|
||||||
|
@ -12,38 +12,38 @@ App.Auth = function(jQuery, api, appState) {
|
||||||
response.json.token.name,
|
response.json.token.name,
|
||||||
remember ? { expires: 365 } : {});
|
remember ? { expires: 365 } : {});
|
||||||
resolve(response);
|
resolve(response);
|
||||||
}).catch(function(response) {
|
}).fail(function(response) {
|
||||||
reject(response);
|
reject(response);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function loginFromToken(token) {
|
function loginFromToken(token) {
|
||||||
return new Promise(function(resolve, reject) {
|
return promise.make(function(resolve, reject) {
|
||||||
api.post('/login', {token: token})
|
promise.wait(api.post('/login', {token: token}))
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
updateAppState(response);
|
updateAppState(response);
|
||||||
resolve(response);
|
resolve(response);
|
||||||
}).catch(function(response) {
|
}).fail(function(response) {
|
||||||
reject(response);
|
reject(response);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function loginAnonymous() {
|
function loginAnonymous() {
|
||||||
return new Promise(function(resolve, reject) {
|
return promise.make(function(resolve, reject) {
|
||||||
api.post('/login')
|
promise.wait(api.post('/login'))
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
updateAppState(response);
|
updateAppState(response);
|
||||||
resolve(response);
|
resolve(response);
|
||||||
}).catch(function(response) {
|
}).fail(function(response) {
|
||||||
reject(response);
|
reject(response);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function logout() {
|
function logout() {
|
||||||
return new Promise(function(resolve, reject) {
|
return promise.make(function(resolve, reject) {
|
||||||
appState.set('loggedIn', false);
|
appState.set('loggedIn', false);
|
||||||
appState.set('loginToken', null);
|
appState.set('loginToken', null);
|
||||||
jQuery.removeCookie('auth');
|
jQuery.removeCookie('auth');
|
||||||
|
@ -52,7 +52,7 @@ App.Auth = function(jQuery, api, appState) {
|
||||||
};
|
};
|
||||||
|
|
||||||
function tryLoginFromCookie() {
|
function tryLoginFromCookie() {
|
||||||
return new Promise(function(resolve, reject) {
|
return promise.make(function(resolve, reject) {
|
||||||
if (appState.get('loggedIn')) {
|
if (appState.get('loggedIn')) {
|
||||||
resolve();
|
resolve();
|
||||||
return;
|
return;
|
||||||
|
@ -64,9 +64,10 @@ App.Auth = function(jQuery, api, appState) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
loginFromToken(authCookie).then(function(response) {
|
promise.wait(loginFromToken(authCookie))
|
||||||
|
.then(function(response) {
|
||||||
resolve();
|
resolve();
|
||||||
}).catch(function(response) {
|
}).fail(function(response) {
|
||||||
jQuery.removeCookie('auth');
|
jQuery.removeCookie('auth');
|
||||||
reject();
|
reject();
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
var App = App || {};
|
var App = App || {};
|
||||||
|
|
||||||
App.Bootstrap = function(auth, router, util) {
|
App.Bootstrap = function(auth, router, util, promise) {
|
||||||
|
|
||||||
util.initPresenter(function() { return App.DI.get('topNavigationPresenter'); });
|
util.initPresenter(function() { return App.DI.get('topNavigationPresenter'); });
|
||||||
|
|
||||||
auth.tryLoginFromCookie()
|
promise.wait(auth.tryLoginFromCookie())
|
||||||
.then(startRouting)
|
.then(startRouting)
|
||||||
.catch(function(error) {
|
.fail(function(error) {
|
||||||
auth.loginAnonymous()
|
promise.wait(auth.loginAnonymous())
|
||||||
.then(startRouting)
|
.then(startRouting)
|
||||||
.catch(function(response) {
|
.fail(function(response) {
|
||||||
console.log(response);
|
console.log(response);
|
||||||
alert('Fatal authentication error: ' + response.json.error);
|
alert('Fatal authentication error: ' + response.json.error);
|
||||||
});
|
});
|
||||||
|
|
|
@ -8,6 +8,7 @@ App.Presenters.LoginPresenter = function(
|
||||||
messagePresenter,
|
messagePresenter,
|
||||||
auth,
|
auth,
|
||||||
router,
|
router,
|
||||||
|
promise,
|
||||||
appState) {
|
appState) {
|
||||||
|
|
||||||
var $el = jQuery('#content');
|
var $el = jQuery('#content');
|
||||||
|
@ -16,7 +17,7 @@ App.Presenters.LoginPresenter = function(
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
topNavigationPresenter.select('login');
|
topNavigationPresenter.select('login');
|
||||||
util.loadTemplate('login-form').then(function(html) {
|
promise.wait(util.promiseTemplate('login-form')).then(function(html) {
|
||||||
template = _.template(html);
|
template = _.template(html);
|
||||||
if (appState.get('loggedIn'))
|
if (appState.get('loggedIn'))
|
||||||
router.navigateToMainPage();
|
router.navigateToMainPage();
|
||||||
|
@ -46,7 +47,7 @@ App.Presenters.LoginPresenter = function(
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
router.navigateToMainPage();
|
router.navigateToMainPage();
|
||||||
//todo: "redirect" to main page
|
//todo: "redirect" to main page
|
||||||
}).catch(function(response) {
|
}).fail(function(response) {
|
||||||
messagePresenter.showError($messages, response.json && response.json.error || response);
|
messagePresenter.showError($messages, response.json && response.json.error || response);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,17 +6,18 @@ App.Presenters.LogoutPresenter = function(
|
||||||
topNavigationPresenter,
|
topNavigationPresenter,
|
||||||
messagePresenter,
|
messagePresenter,
|
||||||
auth,
|
auth,
|
||||||
|
promise,
|
||||||
router) {
|
router) {
|
||||||
|
|
||||||
var $messages = jQuery('#content');
|
var $messages = jQuery('#content');
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
topNavigationPresenter.select('logout');
|
topNavigationPresenter.select('logout');
|
||||||
auth.logout().then(function() {
|
promise.wait(auth.logout()).then(function() {
|
||||||
$messages.empty();
|
$messages.empty();
|
||||||
var $messageDiv = messagePresenter.showInfo($messages, 'Logged out. <a href="">Back to main page</a>');
|
var $messageDiv = messagePresenter.showInfo($messages, 'Logged out. <a href="">Back to main page</a>');
|
||||||
$messageDiv.find('a').click(mainPageLinkClicked);
|
$messageDiv.find('a').click(mainPageLinkClicked);
|
||||||
}).catch(function(response) {
|
}).fail(function(response) {
|
||||||
messagePresenter.showError($messages, response.json && response.json.error || response);
|
messagePresenter.showError($messages, response.json && response.json.error || response);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
var App = App || {};
|
var App = App || {};
|
||||||
App.Presenters = App.Presenters || {};
|
App.Presenters = App.Presenters || {};
|
||||||
|
|
||||||
App.Presenters.PagedCollectionPresenter = function(api, util) {
|
App.Presenters.PagedCollectionPresenter = function(api, util, promise) {
|
||||||
|
|
||||||
var searchOrder;
|
var searchOrder;
|
||||||
var searchQuery;
|
var searchQuery;
|
||||||
|
@ -20,7 +20,7 @@ App.Presenters.PagedCollectionPresenter = function(api, util) {
|
||||||
backendUri = args.backendUri;
|
backendUri = args.backendUri;
|
||||||
renderCallback = args.renderCallback;
|
renderCallback = args.renderCallback;
|
||||||
|
|
||||||
util.loadTemplate('pager').then(function(html) {
|
promise.wait(util.promiseTemplate('pager')).then(function(html) {
|
||||||
template = _.template(html);
|
template = _.template(html);
|
||||||
//renderCallback({entities: [], totalRecords: 0});
|
//renderCallback({entities: [], totalRecords: 0});
|
||||||
|
|
||||||
|
@ -31,17 +31,19 @@ App.Presenters.PagedCollectionPresenter = function(api, util) {
|
||||||
function changePage(newPageNumber) {
|
function changePage(newPageNumber) {
|
||||||
pageNumber = newPageNumber;
|
pageNumber = newPageNumber;
|
||||||
|
|
||||||
|
promise.wait(
|
||||||
api.get(backendUri, {
|
api.get(backendUri, {
|
||||||
order: searchOrder,
|
order: searchOrder,
|
||||||
query: searchQuery,
|
query: searchQuery,
|
||||||
page: pageNumber
|
page: pageNumber
|
||||||
}).then(function(response) {
|
}))
|
||||||
|
.then(function(response) {
|
||||||
totalRecords = response.json.totalRecords;
|
totalRecords = response.json.totalRecords;
|
||||||
pageSize = response.json.pageSize;
|
pageSize = response.json.pageSize;
|
||||||
renderCallback({
|
renderCallback({
|
||||||
entities: response.json.data,
|
entities: response.json.data,
|
||||||
totalRecords: response.json.totalRecords});
|
totalRecords: response.json.totalRecords});
|
||||||
}).catch(function(response) {
|
}).fail(function(response) {
|
||||||
console.log(Error(response.json && response.json.error || response));
|
console.log(Error(response.json && response.json.error || response));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ App.Presenters = App.Presenters || {};
|
||||||
App.Presenters.RegistrationPresenter = function(
|
App.Presenters.RegistrationPresenter = function(
|
||||||
jQuery,
|
jQuery,
|
||||||
util,
|
util,
|
||||||
|
promise,
|
||||||
topNavigationPresenter,
|
topNavigationPresenter,
|
||||||
messagePresenter,
|
messagePresenter,
|
||||||
api) {
|
api) {
|
||||||
|
@ -13,7 +14,7 @@ App.Presenters.RegistrationPresenter = function(
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
topNavigationPresenter.select('register');
|
topNavigationPresenter.select('register');
|
||||||
util.loadTemplate('registration-form').then(function(html) {
|
promise.wait(util.promiseTemplate('registration-form')).then(function(html) {
|
||||||
template = _.template(html);
|
template = _.template(html);
|
||||||
render();
|
render();
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
var App = App || {};
|
var App = App || {};
|
||||||
App.Presenters = App.Presenters || {};
|
App.Presenters = App.Presenters || {};
|
||||||
|
|
||||||
App.Presenters.TopNavigationPresenter = function(util, jQuery, appState) {
|
App.Presenters.TopNavigationPresenter = function(util, jQuery, appState, promise) {
|
||||||
|
|
||||||
var selectedElement = null;
|
var selectedElement = null;
|
||||||
var $el = jQuery('#top-navigation');
|
var $el = jQuery('#top-navigation');
|
||||||
var template;
|
var template;
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
util.loadTemplate('top-navigation').then(function(html) {
|
promise.wait(util.promiseTemplate('top-navigation')).then(function(html) {
|
||||||
template = _.template(html);
|
template = _.template(html);
|
||||||
render();
|
render();
|
||||||
appState.startObserving('loggedIn', 'top-navigation', loginStateChanged);
|
appState.startObserving('loggedIn', 'top-navigation', loginStateChanged);
|
||||||
|
|
|
@ -4,6 +4,7 @@ App.Presenters = App.Presenters || {};
|
||||||
App.Presenters.UserListPresenter = function(
|
App.Presenters.UserListPresenter = function(
|
||||||
jQuery,
|
jQuery,
|
||||||
util,
|
util,
|
||||||
|
promise,
|
||||||
router,
|
router,
|
||||||
pagedCollectionPresenter,
|
pagedCollectionPresenter,
|
||||||
topNavigationPresenter) {
|
topNavigationPresenter) {
|
||||||
|
@ -17,7 +18,7 @@ App.Presenters.UserListPresenter = function(
|
||||||
topNavigationPresenter.select('users');
|
topNavigationPresenter.select('users');
|
||||||
activeSearchOrder = util.parseComplexRouteArgs(args.searchArgs).order;
|
activeSearchOrder = util.parseComplexRouteArgs(args.searchArgs).order;
|
||||||
|
|
||||||
util.loadTemplate('user-list').then(function(html) {
|
promise.wait(util.promiseTemplate('user-list')).then(function(html) {
|
||||||
template = _.template(html);
|
template = _.template(html);
|
||||||
|
|
||||||
pagedCollectionPresenter.init({
|
pagedCollectionPresenter.init({
|
||||||
|
@ -37,7 +38,6 @@ App.Presenters.UserListPresenter = function(
|
||||||
}));
|
}));
|
||||||
$el.find('.order a').click(orderLinkClicked);
|
$el.find('.order a').click(orderLinkClicked);
|
||||||
$el.find('.order [data-order="' + activeSearchOrder + '"]').parent('li').addClass('active');
|
$el.find('.order [data-order="' + activeSearchOrder + '"]').parent('li').addClass('active');
|
||||||
console.log(activeSearchOrder);
|
|
||||||
|
|
||||||
var $pager = $el.find('.pager');
|
var $pager = $el.find('.pager');
|
||||||
pagedCollectionPresenter.render($pager);
|
pagedCollectionPresenter.render($pager);
|
||||||
|
|
28
public_html/js/Promise.js
Normal file
28
public_html/js/Promise.js
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
var App = App || {};
|
||||||
|
|
||||||
|
App.Promise = (function(jQuery) {
|
||||||
|
|
||||||
|
function make(callback)
|
||||||
|
{
|
||||||
|
var deferred = jQuery.Deferred();
|
||||||
|
callback(deferred.resolve, deferred.reject);
|
||||||
|
return deferred.promise();
|
||||||
|
}
|
||||||
|
|
||||||
|
function wait(promise) {
|
||||||
|
return jQuery.when(promise);
|
||||||
|
}
|
||||||
|
|
||||||
|
function waitAll() {
|
||||||
|
return jQuery.when.apply(jQuery, arguments);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
make: make,
|
||||||
|
wait: wait,
|
||||||
|
waitAll: waitAll,
|
||||||
|
};
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
App.DI.registerSingleton('promise', App.Promise);
|
|
@ -1,6 +1,6 @@
|
||||||
var App = App || {};
|
var App = App || {};
|
||||||
|
|
||||||
App.Util = (function(jQuery) {
|
App.Util = (function(jQuery, promise) {
|
||||||
|
|
||||||
var templateCache = {};
|
var templateCache = {};
|
||||||
|
|
||||||
|
@ -28,30 +28,6 @@ App.Util = (function(jQuery) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadTemplate(templateName) {
|
|
||||||
return loadTemplateFromCache(templateName)
|
|
||||||
|| loadTemplateFromDOM(templateName)
|
|
||||||
|| loadTemplateWithAJAX(templateName);
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadTemplateFromCache(templateName) {
|
|
||||||
if (templateName in templateCache) {
|
|
||||||
return new Promise(function(resolve, reject) {
|
|
||||||
resolve(templateCache[templateName]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadTemplateFromDOM(templateName) {
|
|
||||||
var $template = jQuery('#' + templateName + '-template');
|
|
||||||
if ($template.length) {
|
|
||||||
return new Promise(function(resolve, reject) {
|
|
||||||
resolve($template.html());
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function initPresenter(presenterGetter, args) {
|
function initPresenter(presenterGetter, args) {
|
||||||
var presenter = presenterGetter();
|
var presenter = presenterGetter();
|
||||||
presenter.init.call(presenter, args);
|
presenter.init.call(presenter, args);
|
||||||
|
@ -62,8 +38,32 @@ App.Util = (function(jQuery) {
|
||||||
initPresenter(presenterGetter, args);
|
initPresenter(presenterGetter, args);
|
||||||
};
|
};
|
||||||
|
|
||||||
function loadTemplateWithAJAX(templateName) {
|
function promiseTemplate(templateName) {
|
||||||
return new Promise(function(resolve, reject) {
|
return promiseTemplateFromCache(templateName)
|
||||||
|
|| promiseTemplateFromDOM(templateName)
|
||||||
|
|| promiseTemplateWithAJAX(templateName);
|
||||||
|
}
|
||||||
|
|
||||||
|
function promiseTemplateFromCache(templateName) {
|
||||||
|
if (templateName in templateCache) {
|
||||||
|
return promise.make(function(resolve, reject) {
|
||||||
|
resolve(templateCache[templateName]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function promiseTemplateFromDOM(templateName) {
|
||||||
|
var $template = jQuery('#' + templateName + '-template');
|
||||||
|
if ($template.length) {
|
||||||
|
return promise.make(function(resolve, reject) {
|
||||||
|
resolve($template.html());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function promiseTemplateWithAJAX(templateName) {
|
||||||
|
return promise.make(function(resolve, reject) {
|
||||||
var templatesDir = '/templates';
|
var templatesDir = '/templates';
|
||||||
var templateUrl = templatesDir + '/' + templateName + '.tpl';
|
var templateUrl = templatesDir + '/' + templateName + '.tpl';
|
||||||
var templateString;
|
var templateString;
|
||||||
|
@ -83,7 +83,7 @@ App.Util = (function(jQuery) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
loadTemplate: loadTemplate,
|
promiseTemplate: promiseTemplate,
|
||||||
initPresenter : initPresenter,
|
initPresenter : initPresenter,
|
||||||
initContentPresenter: initContentPresenter,
|
initContentPresenter: initContentPresenter,
|
||||||
parseComplexRouteArgs: parseComplexRouteArgs,
|
parseComplexRouteArgs: parseComplexRouteArgs,
|
||||||
|
|
Loading…
Reference in a new issue