Refactor: Less reliance on views for logic for topic list

This commit is contained in:
Robin Ward 2013-05-29 13:28:07 -04:00
parent 4cf1d9c266
commit 9717a344c3
5 changed files with 66 additions and 69 deletions

View file

@ -81,7 +81,53 @@ Discourse.ListTopicsController = Discourse.ObjectController.extend({
// Clear inserted
this.set('content.inserted', Em.A());
return false;
}
},
allLoaded: function() {
return !this.get('loading') && !this.get('more_topics_url');
}.property('loading', 'more_topics_url'),
canCreateTopic: Em.computed.alias('controllers.list.canCreateTopic'),
footerMessage: function() {
if (!this.get('allLoaded')) return;
var content = this.get('category');
if( content ) {
return Em.String.i18n('topics.bottom.category', {category: content.get('name')});
} else {
var split = this.get('filter').split('/');
if (this.get('topics.length') === 0) {
return Em.String.i18n("topics.none." + split[0], {
category: split[1]
});
} else {
return Em.String.i18n("topics.bottom." + split[0], {
category: split[1]
});
}
}
}.property('allLoaded', 'topics.length'),
insertedCount: function() {
var insertedLength = this.get('inserted.length');
if (!insertedLength) return 0;
return insertedLength;
}.property('inserted.length'),
rollUp: function() {
return this.get('insertedCount') > Discourse.SiteSettings.new_topics_rollup;
}.property('insertedCount'),
loadMore: function() {
this.set('loadingMore', true);
var listTopicsController = this;
return this.get('model').loadMoreTopics().then(function(hasMoreTopics) {
listTopicsController.set('loadingMore', false);
return hasMoreTopics;
});
},
});

View file

@ -40,7 +40,10 @@ Discourse.TopicList = Discourse.Model.extend({
return result.topic_list.more_topics_url;
});
} else {
return null;
// Return a promise indicating no more results
return Ember.Deferred.promise(function (p) {
p.resolve(false);
});
}
},

View file

@ -28,12 +28,12 @@
</tr>
</thead>
{{#if view.rollUp}}
{{#if rollUp}}
<tbody>
<tr>
<td colspan="9">
<div class='alert alert-info'>
{{countI18n new_topics_inserted countBinding="view.insertedCount"}}
{{countI18n new_topics_inserted countBinding="insertedCount"}}
<a href='#' {{action showInserted}}>{{i18n show_new_topics}}</a>
</div>
</td>
@ -54,17 +54,16 @@
</div>
<footer id='topic-list-bottom'>
{{#if view.loading}}
{{#if loadingMore}}
<div class='topics-loading'>{{i18n topic.loading_more}}</div>
{{/if}}
</footer>
<h3>
{{view.footerMessage}}
{{#if view.allLoaded}}
{{footerMessage}}
{{#if allLoaded}}
{{#if latest}}
{{#if view.canCreateTopic}}
{{#if canCreateTopic}}
<a href='#' {{action createTopic}}>{{i18n topic.suggest_create_topic}}</a>
{{/if}}
{{else}}

View file

@ -9,30 +9,11 @@
**/
Discourse.ListTopicsView = Discourse.View.extend(Discourse.Scrolling, {
templateName: 'list/topics',
categoryBinding: 'controller.controllers.list.category',
canCreateTopicBinding: 'controller.controllers.list.canCreateTopic',
loadedMore: false,
currentTopicId: null,
insertedCount: (function() {
var inserted;
inserted = this.get('controller.inserted');
if (!inserted) return 0;
return inserted.length;
}).property('controller.inserted.@each'),
rollUp: (function() {
return this.get('insertedCount') > Discourse.SiteSettings.new_topics_rollup;
}).property('insertedCount'),
willDestroyElement: function() {
this.unbindScrolling();
},
allLoaded: (function() {
return !this.get('loading') && !this.get('controller.content.more_topics_url');
}).property('loading', 'controller.content.more_topics_url'),
didInsertElement: function() {
this.bindScrolling();
var eyeline = new Discourse.Eyeline('.topic-list-item');
@ -53,29 +34,18 @@ Discourse.ListTopicsView = Discourse.View.extend(Discourse.Scrolling, {
});
}
this.set('eyeline', eyeline);
this.set('currentTopicId', null);
},
loadMore: function() {
if (this.get('loading')) return;
this.set('loading', true);
var listTopicsView = this;
var promise = this.get('controller.content').loadMoreTopics();
if (promise) {
promise.then(function(hasMoreResults) {
listTopicsView.set('loadedMore', true);
listTopicsView.set('loading', false);
Em.run.schedule('afterRender', function() {
listTopicsView.saveScrollPos();
});
if (!hasMoreResults) {
listTopicsView.get('eyeline').flushRest();
}
listTopicsView.get('controller').loadMore().then(function (hasMoreResults) {
Em.run.schedule('afterRender', function() {
listTopicsView.saveScrollPos();
});
} else {
this.set('loading', false);
}
if (!hasMoreResults) {
listTopicsView.get('eyeline').flushRest();
}
})
},
// Remember where we were scrolled to
@ -88,30 +58,8 @@ Discourse.ListTopicsView = Discourse.View.extend(Discourse.Scrolling, {
var _ref;
this.saveScrollPos();
return (_ref = this.get('eyeline')) ? _ref.update() : void 0;
},
}
footerMessage: function() {
var content, split;
if (!this.get('allLoaded')) {
return;
}
content = this.get('category');
if( content ) {
return Em.String.i18n('topics.bottom.category', {category: content.get('name')});
} else {
content = this.get('controller.content');
split = content.get('filter').split('/');
if (content.get('topics.length') === 0) {
return Em.String.i18n("topics.none." + split[0], {
category: split[1]
});
} else {
return Em.String.i18n("topics.bottom." + split[0], {
category: split[1]
});
}
}
}.property('allLoaded', 'controller.content.topics.length')
});

View file

@ -239,6 +239,7 @@ class TopicQuery
end
end
result = result.listable_topics.includes(category: :topic_only_relative_url)
result = result.where('categories.name is null or categories.name <> ?', query_opts[:exclude_category]) if query_opts[:exclude_category]
result = result.where('categories.name = ?', query_opts[:only_category]) if query_opts[:only_category]