Gracefully handle discussions with no posts

Although this should never happen
This commit is contained in:
Toby Zerner 2015-06-23 10:30:06 +09:30
parent 93b865efd1
commit dfe1a9bae5
4 changed files with 9 additions and 9 deletions

View File

@ -135,7 +135,7 @@ export default class PostScrubber extends Component {
// seen if the browser were scrolled right up to the top of the page, // seen if the browser were scrolled right up to the top of the page,
// and the viewport had a height of 0. // and the viewport had a height of 0.
var $items = stream.$('> .item[data-index]'); var $items = stream.$('> .item[data-index]');
var index = $items.first().data('index'); var index = $items.first().data('index') || 0;
var visible = 0; var visible = 0;
var period = ''; var period = '';
@ -262,7 +262,7 @@ export default class PostScrubber extends Component {
var percentPerPost = this.percentPerPost(); var percentPerPost = this.percentPerPost();
var index = this.index(); var index = this.index();
var count = this.count(); var count = this.count();
var visible = this.visible(); var visible = this.visible() || 1;
var $scrubber = this.$(); var $scrubber = this.$();
$scrubber.find('.index').text(formatNumber(this.visibleIndex())); $scrubber.find('.index').text(formatNumber(this.visibleIndex()));
@ -292,7 +292,7 @@ export default class PostScrubber extends Component {
*/ */
percentPerPost() { percentPerPost() {
var count = this.count() || 1; var count = this.count() || 1;
var visible = this.visible(); var visible = this.visible() || 1;
// To stop the slider of the scrollbar from getting too small when there // To stop the slider of the scrollbar from getting too small when there
// are many posts, we define a minimum percentage height for the slider // are many posts, we define a minimum percentage height for the slider

View File

@ -128,7 +128,7 @@ class PostStream extends mixin(Component, evented) {
*/ */
setup(posts) { setup(posts) {
this.posts = posts; this.posts = posts;
this.visibleStart = this.discussion.postIds().indexOf(posts[0].id()); this.visibleStart = posts.length ? this.discussion.postIds().indexOf(posts[0].id()) : 0;
this.visibleEnd = this.visibleStart + posts.length; this.visibleEnd = this.visibleStart + posts.length;
} }
@ -137,7 +137,7 @@ class PostStream extends mixin(Component, evented) {
*/ */
clear(start, end) { clear(start, end) {
this.visibleStart = start || 0; this.visibleStart = start || 0;
this.visibleEnd = end || this.constructor.loadCount; this.visibleEnd = Math.min(this.count(), end || this.constructor.loadCount);
this.posts = []; this.posts = [];
for (var i = this.visibleStart; i < this.visibleEnd; i++) { for (var i = this.visibleStart; i < this.visibleEnd; i++) {
this.posts.push(null); this.posts.push(null);
@ -234,7 +234,7 @@ class PostStream extends mixin(Component, evented) {
if (this.visibleStart > 0) { if (this.visibleStart > 0) {
var $item = this.$('.item[data-index='+this.visibleStart+']'); var $item = this.$('.item[data-index='+this.visibleStart+']');
if ($item.offset().top > viewportTop - loadAheadDistance) { if ($item.length && $item.offset().top > viewportTop - loadAheadDistance) {
this.loadPrevious(); this.loadPrevious();
} }
} }
@ -242,7 +242,7 @@ class PostStream extends mixin(Component, evented) {
if (this.visibleEnd < this.count()) { if (this.visibleEnd < this.count()) {
var $item = this.$('.item[data-index='+(this.visibleEnd - 1)+']'); var $item = this.$('.item[data-index='+(this.visibleEnd - 1)+']');
if ($item.offset().top + $item.outerHeight(true) < viewportTop + viewportHeight + loadAheadDistance) { if ($item.length && $item.offset().top + $item.outerHeight(true) < viewportTop + viewportHeight + loadAheadDistance) {
this.loadNext(); this.loadNext();
} }
} }

View File

@ -58,7 +58,7 @@ Discussion.prototype.canRename = Model.prop('canRename');
Discussion.prototype.canDelete = Model.prop('canDelete'); Discussion.prototype.canDelete = Model.prop('canDelete');
Discussion.prototype.commentsCount = Model.prop('commentsCount'); Discussion.prototype.commentsCount = Model.prop('commentsCount');
Discussion.prototype.repliesCount = computed('commentsCount', commentsCount => commentsCount - 1); Discussion.prototype.repliesCount = computed('commentsCount', commentsCount => Math.max(0, commentsCount - 1));
Discussion.prototype.posts = Model.many('posts'); Discussion.prototype.posts = Model.many('posts');
Discussion.prototype.postIds = function() { return this.data().links.posts.linkage.map((link) => link.id); }; Discussion.prototype.postIds = function() { return this.data().links.posts.linkage.map((link) => link.id); };

View File

@ -58,7 +58,7 @@ abstract class BaseSerializer extends SerializerAbstract
$data = $model->getRelation($relation); $data = $model->getRelation($relation);
} elseif ($many) { } elseif ($many) {
$relationIds = $relation.'_ids'; $relationIds = $relation.'_ids';
$data = $model->$relationIds ?: $model->$relation()->lists('id'); $data = isset($model->$relationIds) ? $model->$relationIds : $model->$relation()->lists('id');
} else { } else {
$relationId = $relation.'_id'; $relationId = $relation.'_id';
$data = $model->$relationId; $data = $model->$relationId;