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 // Clear inserted
this.set('content.inserted', Em.A()); this.set('content.inserted', Em.A());
return false; 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; return result.topic_list.more_topics_url;
}); });
} else { } 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> </tr>
</thead> </thead>
{{#if view.rollUp}} {{#if rollUp}}
<tbody> <tbody>
<tr> <tr>
<td colspan="9"> <td colspan="9">
<div class='alert alert-info'> <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> <a href='#' {{action showInserted}}>{{i18n show_new_topics}}</a>
</div> </div>
</td> </td>
@ -54,17 +54,16 @@
</div> </div>
<footer id='topic-list-bottom'> <footer id='topic-list-bottom'>
{{#if view.loading}} {{#if loadingMore}}
<div class='topics-loading'>{{i18n topic.loading_more}}</div> <div class='topics-loading'>{{i18n topic.loading_more}}</div>
{{/if}} {{/if}}
</footer> </footer>
<h3> <h3>
{{view.footerMessage}} {{footerMessage}}
{{#if view.allLoaded}} {{#if allLoaded}}
{{#if latest}} {{#if latest}}
{{#if view.canCreateTopic}} {{#if canCreateTopic}}
<a href='#' {{action createTopic}}>{{i18n topic.suggest_create_topic}}</a> <a href='#' {{action createTopic}}>{{i18n topic.suggest_create_topic}}</a>
{{/if}} {{/if}}
{{else}} {{else}}

View File

@ -9,30 +9,11 @@
**/ **/
Discourse.ListTopicsView = Discourse.View.extend(Discourse.Scrolling, { Discourse.ListTopicsView = Discourse.View.extend(Discourse.Scrolling, {
templateName: 'list/topics', 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() { willDestroyElement: function() {
this.unbindScrolling(); this.unbindScrolling();
}, },
allLoaded: (function() {
return !this.get('loading') && !this.get('controller.content.more_topics_url');
}).property('loading', 'controller.content.more_topics_url'),
didInsertElement: function() { didInsertElement: function() {
this.bindScrolling(); this.bindScrolling();
var eyeline = new Discourse.Eyeline('.topic-list-item'); 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('eyeline', eyeline);
this.set('currentTopicId', null);
}, },
loadMore: function() { loadMore: function() {
if (this.get('loading')) return;
this.set('loading', true);
var listTopicsView = this; var listTopicsView = this;
var promise = this.get('controller.content').loadMoreTopics(); listTopicsView.get('controller').loadMore().then(function (hasMoreResults) {
if (promise) {
promise.then(function(hasMoreResults) {
listTopicsView.set('loadedMore', true);
listTopicsView.set('loading', false);
Em.run.schedule('afterRender', function() { Em.run.schedule('afterRender', function() {
listTopicsView.saveScrollPos(); listTopicsView.saveScrollPos();
}); });
if (!hasMoreResults) { if (!hasMoreResults) {
listTopicsView.get('eyeline').flushRest(); listTopicsView.get('eyeline').flushRest();
} }
}); })
} else {
this.set('loading', false);
}
}, },
// Remember where we were scrolled to // Remember where we were scrolled to
@ -88,30 +58,8 @@ Discourse.ListTopicsView = Discourse.View.extend(Discourse.Scrolling, {
var _ref; var _ref;
this.saveScrollPos(); this.saveScrollPos();
return (_ref = this.get('eyeline')) ? _ref.update() : void 0; 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
end end
result = result.listable_topics.includes(category: :topic_only_relative_url) 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 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] result = result.where('categories.name = ?', query_opts[:only_category]) if query_opts[:only_category]