From 720101b3ee89b92a016e4875e213d50cbb4e1135 Mon Sep 17 00:00:00 2001 From: Daniel Waterworth Date: Mon, 25 Nov 2019 16:42:01 +0000 Subject: [PATCH] FIX: Update site data when we receive a list of categories When we receive a list of categories, we should store them so that we are able to query them in the browser without a page refresh. This removes a previous fix for the same issue that was much less general. --- .../discourse/models/category-list.js.es6 | 5 +++- .../javascripts/discourse/models/site.js.es6 | 4 +++- .../routes/build-category-route.js.es6 | 23 ++----------------- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/app/assets/javascripts/discourse/models/category-list.js.es6 b/app/assets/javascripts/discourse/models/category-list.js.es6 index f04b40dff80..b8437f706dd 100644 --- a/app/assets/javascripts/discourse/models/category-list.js.es6 +++ b/app/assets/javascripts/discourse/models/category-list.js.es6 @@ -2,6 +2,7 @@ import PreloadStore from "preload-store"; import { ajax } from "discourse/lib/ajax"; import Topic from "discourse/models/topic"; import Category from "discourse/models/category"; +import Site from "discourse/models/site"; const CategoryList = Ember.ArrayProxy.extend({ init() { @@ -76,7 +77,9 @@ CategoryList.reopenClass({ break; } - categories.pushObject(store.createRecord("category", c)); + const record = Site.current().updateCategory(c); + record.setupGroupsAndPermissions(); + categories.pushObject(record); }); return categories; }, diff --git a/app/assets/javascripts/discourse/models/site.js.es6 b/app/assets/javascripts/discourse/models/site.js.es6 index aa7a2b5ded6..5f9065d3d88 100644 --- a/app/assets/javascripts/discourse/models/site.js.es6 +++ b/app/assets/javascripts/discourse/models/site.js.es6 @@ -87,7 +87,7 @@ const Site = RestModel.extend({ }, // Returns it in the correct order, by setting - @discourseComputed + @discourseComputed("categories.[]") categoriesList() { return this.siteSettings.fixed_category_positions ? this.categories @@ -123,11 +123,13 @@ const Site = RestModel.extend({ if (existingCategory) { existingCategory.setProperties(newCategory); + return existingCategory; } else { // TODO insert in right order? newCategory = this.store.createRecord("category", newCategory); categories.pushObject(newCategory); this.categoriesById[categoryId] = newCategory; + return newCategory; } } }); diff --git a/app/assets/javascripts/discourse/routes/build-category-route.js.es6 b/app/assets/javascripts/discourse/routes/build-category-route.js.es6 index b19c91b34f5..d360c11eb05 100644 --- a/app/assets/javascripts/discourse/routes/build-category-route.js.es6 +++ b/app/assets/javascripts/discourse/routes/build-category-route.js.es6 @@ -21,28 +21,9 @@ export default (filterArg, params) => { modelParams.slug, modelParams.parentSlug ); - if (!category) { - return Category.reloadBySlug( - modelParams.slug, - modelParams.parentSlug - ).then(atts => { - if (modelParams.parentSlug) { - atts.category.parentCategory = Category.findBySlug( - modelParams.parentSlug - ); - } - const record = this.store.createRecord("category", atts.category); - record.setupGroupsAndPermissions(); - this.site.updateCategory(record); - return { - category: Category.findBySlug( - modelParams.slug, - modelParams.parentSlug - ) - }; - }); + if (category) { + return { category }; } - return { category }; }, afterModel(model, transition) {