A platform for community discussion. Free, open, simple.
REFACTOR: topic-list model (#7658)
Joffrey JAFFEUX1db7fd6f9b66
diff --git a/app/assets/javascripts/discourse/models/topic-list.js.es6 b/app/assets/javascripts/discourse/models/topic-list.js.es6index 9ed73dadca9..e12a90e77e1 100644--- a/app/assets/javascripts/discourse/models/topic-list.js.es6+++ b/app/assets/javascripts/discourse/models/topic-list.js.es6@@ -1,15 +1,17 @@import { ajax } from "discourse/lib/ajax";import RestModel from "discourse/models/rest";import Model from "discourse/models/model";+import { getOwner } from "discourse-common/lib/get-owner";// Whether to show the category badge in topic listsfunction displayCategoryInList(site, category) {if (category) {- if (category.get("has_children")) {+ if (category.has_children) {return true;}- let draftCategoryId = site.get("shared_drafts_category_id");- if (draftCategoryId && category.get("id") === draftCategoryId) {++ const draftCategoryId = site.shared_drafts_category_id;+ if (draftCategoryId && category.id === draftCategoryId) {return true;}@@ -25,7 +27,7 @@ const TopicList = RestModel.extend({forEachNew(topics, callback) {const topicIds = [];- this.topics.forEach(topic => (topicIds[topic.get("id")] = true));+ this.topics.forEach(topic => (topicIds[topic.id] = true));topics.forEach(topic => {if (!topicIds[topic.id]) {@@ -68,30 +70,27 @@ const TopicList = RestModel.extend({moreUrl += "?" + params;}- const self = this;this.set("loadingMore", true);- const store = this.store;- return ajax({ url: moreUrl }).then(function(result) {+ return ajax({ url: moreUrl }).then(result => {let topicsAdded = 0;if (result) {// the new topics loaded from the server- const newTopics = TopicList.topicsFrom(store, result);- const topics = self.get("topics");+ const newTopics = TopicList.topicsFrom(this.store, result);- self.forEachNew(newTopics, function(t) {+ this.forEachNew(newTopics, t => {t.set("highlight", topicsAdded++ === 0);- topics.pushObject(t);+ this.topics.pushObject(t);});- self.setProperties({+ this.setProperties({loadingMore: false,more_topics_url: result.topic_list.more_topics_url});- Discourse.Session.currentProp("topicList", self);- return self.get("more_topics_url");+ Discourse.Session.currentProp("topicList", this);+ return this.more_topics_url;}});} else {@@ -102,37 +101,32 @@ const TopicList = RestModel.extend({// loads topics with these ids "before" the current topicsloadBefore(topic_ids, storeInSession) {- const topicList = this,- topics = this.topics;-// refresh dupes- topics.removeObjects(- topics.filter(topic => topic_ids.indexOf(topic.get("id")) >= 0)+ this.topics.removeObjects(+ this.topics.filter(topic => topic_ids.indexOf(topic.id) >= 0));- const url = `${Discourse.getURL("/")}${this.get(- "filter"- )}.json?topic_ids=${topic_ids.join(",")}`;- const store = this.store;+ const url = `${Discourse.getURL("/")}${+ this.filter+ }.json?topic_ids=${topic_ids.join(",")}`;return ajax({ url, data: this.params }).then(result => {let i = 0;- topicList.forEachNew(TopicList.topicsFrom(store, result), function(t) {+ this.forEachNew(TopicList.topicsFrom(this.store, result), t => {// highlight the first of the new topics so we can get a visual feedbackt.set("highlight", true);- topics.insertAt(i, t);+ this.topics.insertAt(i, t);i++;});- if (storeInSession) Discourse.Session.currentProp("topicList", topicList);++ if (storeInSession) Discourse.Session.currentProp("topicList", this);});}});TopicList.reopenClass({topicsFrom(store, result, opts) {- if (!result) {- return;- }+ if (!result) return;opts = opts || {};let listKey = opts.listKey || "topics";@@ -143,9 +137,9 @@ TopicList.reopenClass({users = Model.extractByKey(result.users, Discourse.User),groups = Model.extractByKey(result.primary_groups, Ember.Object);- return result.topic_list[listKey].map(function(t) {+ return result.topic_list[listKey].map(t => {t.category = categories.findBy("id", t.category_id);- t.posters.forEach(function(p) {+ t.posters.forEach(p => {p.user = users[p.user_id];p.extraClasses = p.extras;if (p.primary_group_id) {@@ -157,11 +151,11 @@ TopicList.reopenClass({}}});+if (t.participants) {- t.participants.forEach(function(p) {- p.user = users[p.user_id];- });+ t.participants.forEach(p => (p.user = users[p.user_id]));}+return store.createRecord("topic", t);});},@@ -188,7 +182,7 @@ TopicList.reopenClass({},find(filter, params) {- const store = Discourse.__container__.lookup("service:store");+ const store = getOwner(this).lookup("service:store");return store.findFiltered("topicList", { filter, params });},