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 || {};
App.API = function(jQuery, promise, appState) {
App.API = function(_, jQuery, promise, appState) {
var baseUrl = '/api/';
@ -32,13 +32,13 @@ App.API = function(jQuery, promise, appState) {
success: function(data, textStatus, xhr) {
resolve({
status: xhr.status,
json: data});
json: stripMeta(data)});
},
error: function(xhr, textStatus, errorThrown) {
reject({
status: xhr.status,
json: xhr.responseJSON ?
xhr.responseJSON :
stripMeta(xhr.responseJSON) :
{error: errorThrown}});
},
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 {
get: get,
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,18 +2,21 @@ var App = App || {};
App.Bootstrap = function(auth, router, promise, presenterManager) {
promise.wait(
auth.tryLoginFromCookie(),
presenterManager.init())
promise.wait(presenterManager.init())
.then(function() {
promise.wait(auth.tryLoginFromCookie())
.then(startRouting)
.fail(function(error) {
promise.wait(auth.loginAnonymous())
.then(startRouting)
.fail(function(response) {
console.log(response);
window.alert('Fatal authentication error: ' + response.json.error);
.fail(function() {
console.log(arguments);
window.alert('Fatal authentication error');
});
});
}).fail(function() {
console.log(arguments);
});
function startRouting() {
try {

View file

@ -20,8 +20,7 @@ App.Presenters.GlobalCommentListPresenter = function(
util.promiseTemplate('global-comment-list'),
util.promiseTemplate('global-comment-list-item'),
util.promiseTemplate('post-list-item'))
.then(function(listTemplate, listItemTemplate, postTemplate)
{
.then(function(listTemplate, listItemTemplate, postTemplate) {
templates.list = listTemplate;
templates.listItem = listItemTemplate;
templates.post = postTemplate;
@ -40,8 +39,10 @@ App.Presenters.GlobalCommentListPresenter = 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;
globals = globalsResponse.json;
post = featuredPostResponse.json;
post = featuredPostResponse.json.id ? featuredPostResponse.json : null;
render();
loaded();

View file

@ -29,6 +29,9 @@ App.Presenters.LoginPresenter = function(
$el.find('input:eq(0)').focus();
}
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>');
$messageDiv.find('a').click(mainPageLinkClicked);
}).fail(function(response) {
loaded();
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;
render();
loaded();
}).fail(function() {
console.log(arguments);
loaded();
});
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -20,7 +20,7 @@ App.Promise = function(_, jQuery) {
active.push(promise.promiseId);
promise.then(function() {
promise.always(function() {
if (!_.contains(active, 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) {
if (!number) {
if (!number && number !== 0) {
return NaN;
}
number *= 1.0;

View file

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