Fixed promises on Internet Explorer

This commit is contained in:
Marcin Kurczewski 2014-09-04 18:06:25 +02:00
parent ee9fde5402
commit 840039958a
12 changed files with 112 additions and 77 deletions

View file

@ -28,6 +28,7 @@
</div>
<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/Api.js"></script>
<script type="text/javascript" src="/js/Auth.js"></script>

View file

@ -1,6 +1,6 @@
var App = App || {};
App.API = function() {
App.API = function(promise) {
var baseUrl = '/api/';
@ -24,7 +24,7 @@ App.API = function() {
var fullUrl = baseUrl + '/' + url;
fullUrl = fullUrl.replace(/\/{2,}/, '/');
return new Promise(function(resolve, reject) {
return promise.make(function(resolve, reject) {
$.ajax({
success: function(data, textStatus, xhr) {
resolve({

View file

@ -1,10 +1,10 @@
var App = App || {};
App.Auth = function(jQuery, api, appState) {
App.Auth = function(jQuery, util, api, appState, promise) {
function loginFromCredentials(userName, password, remember) {
return new Promise(function(resolve, reject) {
api.post('/login', {userName: userName, password: password})
return promise.make(function(resolve, reject) {
promise.wait(api.post('/login', {userName: userName, password: password}))
.then(function(response) {
updateAppState(response);
jQuery.cookie(
@ -12,38 +12,38 @@ App.Auth = function(jQuery, api, appState) {
response.json.token.name,
remember ? { expires: 365 } : {});
resolve(response);
}).catch(function(response) {
}).fail(function(response) {
reject(response);
});
});
};
function loginFromToken(token) {
return new Promise(function(resolve, reject) {
api.post('/login', {token: token})
return promise.make(function(resolve, reject) {
promise.wait(api.post('/login', {token: token}))
.then(function(response) {
updateAppState(response);
resolve(response);
}).catch(function(response) {
}).fail(function(response) {
reject(response);
});
});
};
function loginAnonymous() {
return new Promise(function(resolve, reject) {
api.post('/login')
return promise.make(function(resolve, reject) {
promise.wait(api.post('/login'))
.then(function(response) {
updateAppState(response);
resolve(response);
}).catch(function(response) {
}).fail(function(response) {
reject(response);
});
});
};
function logout() {
return new Promise(function(resolve, reject) {
return promise.make(function(resolve, reject) {
appState.set('loggedIn', false);
appState.set('loginToken', null);
jQuery.removeCookie('auth');
@ -52,7 +52,7 @@ App.Auth = function(jQuery, api, appState) {
};
function tryLoginFromCookie() {
return new Promise(function(resolve, reject) {
return promise.make(function(resolve, reject) {
if (appState.get('loggedIn')) {
resolve();
return;
@ -64,9 +64,10 @@ App.Auth = function(jQuery, api, appState) {
return;
}
loginFromToken(authCookie).then(function(response) {
promise.wait(loginFromToken(authCookie))
.then(function(response) {
resolve();
}).catch(function(response) {
}).fail(function(response) {
jQuery.removeCookie('auth');
reject();
});

View file

@ -1,15 +1,15 @@
var App = App || {};
App.Bootstrap = function(auth, router, util) {
App.Bootstrap = function(auth, router, util, promise) {
util.initPresenter(function() { return App.DI.get('topNavigationPresenter'); });
auth.tryLoginFromCookie()
promise.wait(auth.tryLoginFromCookie())
.then(startRouting)
.catch(function(error) {
auth.loginAnonymous()
.fail(function(error) {
promise.wait(auth.loginAnonymous())
.then(startRouting)
.catch(function(response) {
.fail(function(response) {
console.log(response);
alert('Fatal authentication error: ' + response.json.error);
});

View file

@ -8,6 +8,7 @@ App.Presenters.LoginPresenter = function(
messagePresenter,
auth,
router,
promise,
appState) {
var $el = jQuery('#content');
@ -16,7 +17,7 @@ App.Presenters.LoginPresenter = function(
function init() {
topNavigationPresenter.select('login');
util.loadTemplate('login-form').then(function(html) {
promise.wait(util.promiseTemplate('login-form')).then(function(html) {
template = _.template(html);
if (appState.get('loggedIn'))
router.navigateToMainPage();
@ -46,7 +47,7 @@ App.Presenters.LoginPresenter = function(
.then(function(response) {
router.navigateToMainPage();
//todo: "redirect" to main page
}).catch(function(response) {
}).fail(function(response) {
messagePresenter.showError($messages, response.json && response.json.error || response);
});
}

View file

@ -6,17 +6,18 @@ App.Presenters.LogoutPresenter = function(
topNavigationPresenter,
messagePresenter,
auth,
promise,
router) {
var $messages = jQuery('#content');
function init() {
topNavigationPresenter.select('logout');
auth.logout().then(function() {
promise.wait(auth.logout()).then(function() {
$messages.empty();
var $messageDiv = messagePresenter.showInfo($messages, 'Logged out. <a href="">Back to main page</a>');
$messageDiv.find('a').click(mainPageLinkClicked);
}).catch(function(response) {
}).fail(function(response) {
messagePresenter.showError($messages, response.json && response.json.error || response);
});
}

View file

@ -1,7 +1,7 @@
var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.PagedCollectionPresenter = function(api, util) {
App.Presenters.PagedCollectionPresenter = function(api, util, promise) {
var searchOrder;
var searchQuery;
@ -20,7 +20,7 @@ App.Presenters.PagedCollectionPresenter = function(api, util) {
backendUri = args.backendUri;
renderCallback = args.renderCallback;
util.loadTemplate('pager').then(function(html) {
promise.wait(util.promiseTemplate('pager')).then(function(html) {
template = _.template(html);
//renderCallback({entities: [], totalRecords: 0});
@ -31,17 +31,19 @@ App.Presenters.PagedCollectionPresenter = function(api, util) {
function changePage(newPageNumber) {
pageNumber = newPageNumber;
promise.wait(
api.get(backendUri, {
order: searchOrder,
query: searchQuery,
page: pageNumber
}).then(function(response) {
}))
.then(function(response) {
totalRecords = response.json.totalRecords;
pageSize = response.json.pageSize;
renderCallback({
entities: response.json.data,
totalRecords: response.json.totalRecords});
}).catch(function(response) {
}).fail(function(response) {
console.log(Error(response.json && response.json.error || response));
});
}

View file

@ -4,6 +4,7 @@ App.Presenters = App.Presenters || {};
App.Presenters.RegistrationPresenter = function(
jQuery,
util,
promise,
topNavigationPresenter,
messagePresenter,
api) {
@ -13,7 +14,7 @@ App.Presenters.RegistrationPresenter = function(
function init() {
topNavigationPresenter.select('register');
util.loadTemplate('registration-form').then(function(html) {
promise.wait(util.promiseTemplate('registration-form')).then(function(html) {
template = _.template(html);
render();
});

View file

@ -1,14 +1,14 @@
var App = App || {};
App.Presenters = App.Presenters || {};
App.Presenters.TopNavigationPresenter = function(util, jQuery, appState) {
App.Presenters.TopNavigationPresenter = function(util, jQuery, appState, promise) {
var selectedElement = null;
var $el = jQuery('#top-navigation');
var template;
function init() {
util.loadTemplate('top-navigation').then(function(html) {
promise.wait(util.promiseTemplate('top-navigation')).then(function(html) {
template = _.template(html);
render();
appState.startObserving('loggedIn', 'top-navigation', loginStateChanged);

View file

@ -4,6 +4,7 @@ App.Presenters = App.Presenters || {};
App.Presenters.UserListPresenter = function(
jQuery,
util,
promise,
router,
pagedCollectionPresenter,
topNavigationPresenter) {
@ -17,7 +18,7 @@ App.Presenters.UserListPresenter = function(
topNavigationPresenter.select('users');
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);
pagedCollectionPresenter.init({
@ -37,7 +38,6 @@ App.Presenters.UserListPresenter = function(
}));
$el.find('.order a').click(orderLinkClicked);
$el.find('.order [data-order="' + activeSearchOrder + '"]').parent('li').addClass('active');
console.log(activeSearchOrder);
var $pager = $el.find('.pager');
pagedCollectionPresenter.render($pager);

28
public_html/js/Promise.js Normal file
View 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);

View file

@ -1,6 +1,6 @@
var App = App || {};
App.Util = (function(jQuery) {
App.Util = (function(jQuery, promise) {
var templateCache = {};
@ -28,30 +28,6 @@ App.Util = (function(jQuery) {
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) {
var presenter = presenterGetter();
presenter.init.call(presenter, args);
@ -62,8 +38,32 @@ App.Util = (function(jQuery) {
initPresenter(presenterGetter, args);
};
function loadTemplateWithAJAX(templateName) {
return new Promise(function(resolve, reject) {
function promiseTemplate(templateName) {
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 templateUrl = templatesDir + '/' + templateName + '.tpl';
var templateString;
@ -83,7 +83,7 @@ App.Util = (function(jQuery) {
}
return {
loadTemplate: loadTemplate,
promiseTemplate: promiseTemplate,
initPresenter : initPresenter,
initContentPresenter: initContentPresenter,
parseComplexRouteArgs: parseComplexRouteArgs,