Fixed frontend crashing when database is empty

This commit is contained in:
Marcin Kurczewski 2014-10-10 18:13:46 +02:00
parent d2695e635c
commit 0811968718
23 changed files with 102 additions and 40 deletions

View file

@ -1,6 +1,6 @@
var App = App || {}; var App = App || {};
App.API = function(jQuery, promise, appState) { App.API = function(_, jQuery, promise, appState) {
var baseUrl = '/api/'; var baseUrl = '/api/';
@ -32,13 +32,13 @@ App.API = function(jQuery, promise, appState) {
success: function(data, textStatus, xhr) { success: function(data, textStatus, xhr) {
resolve({ resolve({
status: xhr.status, status: xhr.status,
json: data}); json: stripMeta(data)});
}, },
error: function(xhr, textStatus, errorThrown) { error: function(xhr, textStatus, errorThrown) {
reject({ reject({
status: xhr.status, status: xhr.status,
json: xhr.responseJSON ? json: xhr.responseJSON ?
xhr.responseJSON : stripMeta(xhr.responseJSON) :
{error: errorThrown}}); {error: errorThrown}});
}, },
type: method, type: method,
@ -48,6 +48,16 @@ App.API = function(jQuery, promise, appState) {
}); });
} }
function stripMeta(data) {
var result = {};
_.each(data, function(v, k) {
if (!k.match(/^__/)) {
result[k] = v;
}
});
return result;
}
return { return {
get: get, get: get,
post: post, post: post,
@ -57,4 +67,4 @@ App.API = function(jQuery, promise, appState) {
}; };
App.DI.registerSingleton('api', ['jQuery', 'promise', 'appState'], App.API); App.DI.registerSingleton('api', ['_', 'jQuery', 'promise', 'appState'], App.API);

View file

@ -2,17 +2,20 @@ var App = App || {};
App.Bootstrap = function(auth, router, promise, presenterManager) { App.Bootstrap = function(auth, router, promise, presenterManager) {
promise.wait( promise.wait(presenterManager.init())
auth.tryLoginFromCookie(), .then(function() {
presenterManager.init()) promise.wait(auth.tryLoginFromCookie())
.then(startRouting)
.fail(function(error) {
promise.wait(auth.loginAnonymous())
.then(startRouting) .then(startRouting)
.fail(function(response) { .fail(function(error) {
console.log(response); promise.wait(auth.loginAnonymous())
window.alert('Fatal authentication error: ' + response.json.error); .then(startRouting)
.fail(function() {
console.log(arguments);
window.alert('Fatal authentication error');
});
}); });
}).fail(function() {
console.log(arguments);
}); });
function startRouting() { function startRouting() {

View file

@ -20,28 +20,29 @@ App.Presenters.GlobalCommentListPresenter = function(
util.promiseTemplate('global-comment-list'), util.promiseTemplate('global-comment-list'),
util.promiseTemplate('global-comment-list-item'), util.promiseTemplate('global-comment-list-item'),
util.promiseTemplate('post-list-item')) util.promiseTemplate('post-list-item'))
.then(function(listTemplate, listItemTemplate, postTemplate) .then(function(listTemplate, listItemTemplate, postTemplate) {
{ templates.list = listTemplate;
templates.list = listTemplate; templates.listItem = listItemTemplate;
templates.listItem = listItemTemplate; templates.post = postTemplate;
templates.post = postTemplate;
render(); render();
loaded(); loaded();
pagerPresenter.init({ pagerPresenter.init({
baseUri: '#/comments', baseUri: '#/comments',
backendUri: '/comments', backendUri: '/comments',
$target: $el.find('.pagination-target'), $target: $el.find('.pagination-target'),
updateCallback: function(data, clear) { updateCallback: function(data, clear) {
renderPosts(data.entities, clear); renderPosts(data.entities, clear);
},
}, },
function() { },
reinit(params, function() {}); function() {
}); reinit(params, function() {});
}) });
.fail(function() { console.log(new Error(arguments)); }); }).fail(function() {
console.log(arguments);
loaded();
});
} }

View file

@ -33,7 +33,7 @@ App.Presenters.HomePresenter = function(
templates.postContent = postContentTemplate; templates.postContent = postContentTemplate;
globals = globalsResponse.json; globals = globalsResponse.json;
post = featuredPostResponse.json; post = featuredPostResponse.json.id ? featuredPostResponse.json : null;
render(); render();
loaded(); loaded();

View file

@ -29,6 +29,9 @@ App.Presenters.LoginPresenter = function(
$el.find('input:eq(0)').focus(); $el.find('input:eq(0)').focus();
} }
loaded(); loaded();
}).fail(function() {
console.log(arguments);
loaded();
}); });
} }

View file

@ -21,8 +21,8 @@ App.Presenters.LogoutPresenter = function(
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);
}).fail(function(response) { }).fail(function(response) {
loaded();
messagePresenter.showError(($messages, response.json && response.json.error || response) + '<br/>Reload the page to continue.'); messagePresenter.showError(($messages, response.json && response.json.error || response) + '<br/>Reload the page to continue.');
loaded();
}); });
} }

View file

@ -46,6 +46,9 @@ App.Presenters.PagerPresenter = function(
templates.pager = template; templates.pager = template;
render(); render();
loaded(); loaded();
}).fail(function() {
console.log(arguments);
loaded();
}); });
} }

View file

@ -53,10 +53,15 @@ App.Presenters.PostCommentListPresenter = function(
.then(function(response) { .then(function(response) {
comments = response.json.data; comments = response.json.data;
render(); render();
}).fail(function() {
console.log(arguments);
}); });
} }
}) })
.fail(function() { console.log(new Error(arguments)); }); .fail(function() {
console.log(arguments);
loaded();
});
} }
function render() { function render() {

View file

@ -83,7 +83,10 @@ App.Presenters.PostPresenter = function(
topNavigationPresenter.changeTitle('@' + post.id); topNavigationPresenter.changeTitle('@' + post.id);
render(); render();
loaded(); loaded();
}).fail(loaded); }).fail(function() {
console.log(arguments);
loaded();
});
} }
function attachLinksToPostsAround() { function attachLinksToPostsAround() {

View file

@ -32,6 +32,9 @@ App.Presenters.PostUploadPresenter = function(
templates.upload = template; templates.upload = template;
render(); render();
loaded(); loaded();
}).fail(function() {
console.log(arguments);
loaded();
}); });
} }

View file

@ -21,6 +21,9 @@ App.Presenters.RegistrationPresenter = function(
templates.registration = template; templates.registration = template;
render(); render();
loaded(); loaded();
}).fail(function() {
console.log(arguments);
loaded();
}); });
} }

View file

@ -38,6 +38,9 @@ App.Presenters.TagListPresenter = function(
function() { function() {
reinit(params, function() {}); reinit(params, function() {});
}); });
}).fail(function() {
console.log(arguments);
loaded();
}); });
} }

View file

@ -36,6 +36,9 @@ App.Presenters.TagPresenter = function(
templates.postListItem = postListItemTemplate; templates.postListItem = postListItemTemplate;
reinit(params, loaded); reinit(params, loaded);
}).fail(function() {
console.log(arguments);
loaded();
}); });
} }

View file

@ -19,6 +19,8 @@ App.Presenters.TopNavigationPresenter = function(
render(); render();
loaded(); loaded();
auth.startObservingLoginChanges('top-navigation', loginStateChanged); auth.startObservingLoginChanges('top-navigation', loginStateChanged);
}).fail(function() {
loaded();
}); });
} }

View file

@ -28,6 +28,9 @@ App.Presenters.UserAccountRemovalPresenter = function(
templates.accountRemoval = template; templates.accountRemoval = template;
render(); render();
loaded(); loaded();
}).fail(function() {
console.log(arguments);
loaded();
}); });
} }

View file

@ -45,6 +45,9 @@ App.Presenters.UserAccountSettingsPresenter = function(
templates.accountRemoval = template; templates.accountRemoval = template;
render(); render();
loaded(); loaded();
}).fail(function() {
console.log(arguments);
loaded();
}); });
} }

View file

@ -36,6 +36,9 @@ App.Presenters.UserActivationPresenter = function(
} }
render(); render();
loaded(); loaded();
}).fail(function() {
console.log(arguments);
loaded();
}); });
} }

View file

@ -25,6 +25,9 @@ App.Presenters.UserBrowsingSettingsPresenter = function(
templates.browsingSettings = template; templates.browsingSettings = template;
render(); render();
loaded(); loaded();
}).fail(function() {
console.log(arguments);
loaded();
}); });
} }
@ -53,6 +56,8 @@ App.Presenters.UserBrowsingSettingsPresenter = function(
promise.wait(browsingSettings.setSettings(newSettings)) promise.wait(browsingSettings.setSettings(newSettings))
.then(function() { .then(function() {
messagePresenter.showInfo($messages, 'Browsing settings updated!'); messagePresenter.showInfo($messages, 'Browsing settings updated!');
}).fail(function() {
console.log(arguments);
}); });
} }

View file

@ -39,6 +39,9 @@ App.Presenters.UserListPresenter = function(
function() { function() {
reinit(params, function() {}); reinit(params, function() {});
}); });
}).fail(function() {
console.log(arguments);
loaded();
}); });
} }

View file

@ -28,6 +28,9 @@ App.Presenters.UserPresenter = function(
$messages = $el.find('.messages'); $messages = $el.find('.messages');
templates.user = template; templates.user = template;
reinit(params, loaded); reinit(params, loaded);
}).fail(function() {
console.log(arguments);
loaded();
}); });
} }

View file

@ -20,7 +20,7 @@ App.Promise = function(_, jQuery) {
active.push(promise.promiseId); active.push(promise.promiseId);
promise.then(function() { promise.always(function() {
if (!_.contains(active, promise.promiseId)) { if (!_.contains(active, promise.promiseId)) {
throw new Error('Broken promise (promise ID: ' + promise.promiseId + ')'); throw new Error('Broken promise (promise ID: ' + promise.promiseId + ')');
} }

View file

@ -124,7 +124,7 @@ App.Util = function(_, jQuery, marked, promise) {
} }
function formatUnits(number, base, suffixes, callback) { function formatUnits(number, base, suffixes, callback) {
if (!number) { if (!number && number !== 0) {
return NaN; return NaN;
} }
number *= 1.0; number *= 1.0;

View file

@ -1,10 +1,10 @@
<div id="home"> <div id="home">
<h1><%= title %></h1> <h1><%= title %></h1>
<p> <p>
<small>Serving <%= globals.postCount %> posts (<%= formatFileSize(globals.postSize) %>)</small> <small>Serving <%= globals.postCount || 0 %> posts (<%= formatFileSize(globals.postSize || 0) %>)</small>
</p> </p>
<% if (post) { %> <% if (post && typeof(post) !== 'undefined') { %>
<div class="post"> <div class="post">
<%= postContentTemplate({post: post}) %> <%= postContentTemplate({post: post}) %>
<div class="post-footer"> <div class="post-footer">