DEV: Add model transformation hooks in more locations (#24547)
Motivation is https://github.com/discourse/discourse-encrypt/pull/294
This commit is contained in:
parent
21a7ebf1bc
commit
766e0f7b36
|
@ -1,6 +1,7 @@
|
||||||
import RestAdapter from "discourse/adapters/rest";
|
import RestAdapter from "discourse/adapters/rest";
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
import PreloadStore from "discourse/lib/preload-store";
|
import PreloadStore from "discourse/lib/preload-store";
|
||||||
|
import Topic from "discourse/models/topic";
|
||||||
|
|
||||||
export default RestAdapter.extend({
|
export default RestAdapter.extend({
|
||||||
find(store, type, { filter, params }) {
|
find(store, type, { filter, params }) {
|
||||||
|
@ -38,4 +39,10 @@ export default RestAdapter.extend({
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async applyTransformations(results) {
|
||||||
|
for (const topicList of results) {
|
||||||
|
await Topic.applyTransformations(topicList.topics);
|
||||||
|
}
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -96,7 +96,7 @@ export default Controller.extend({
|
||||||
this.set("permissionDenied", true);
|
this.set("permissionDenied", true);
|
||||||
},
|
},
|
||||||
|
|
||||||
_processLoadResponse(searchTerm, response) {
|
async _processLoadResponse(searchTerm, response) {
|
||||||
if (!response || !response.user_bookmark_list) {
|
if (!response || !response.user_bookmark_list) {
|
||||||
this.model.loadMoreUrl = null;
|
this.model.loadMoreUrl = null;
|
||||||
return;
|
return;
|
||||||
|
@ -108,6 +108,7 @@ export default Controller.extend({
|
||||||
|
|
||||||
if (response.bookmarks) {
|
if (response.bookmarks) {
|
||||||
const bookmarkModels = response.bookmarks.map(this.transform);
|
const bookmarkModels = response.bookmarks.map(this.transform);
|
||||||
|
await Bookmark.applyTransformations(bookmarkModels);
|
||||||
this.model.bookmarks.pushObjects(bookmarkModels);
|
this.model.bookmarks.pushObjects(bookmarkModels);
|
||||||
this.session.set("bookmarksModel", this.model);
|
this.session.set("bookmarksModel", this.model);
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import Site from "discourse/models/site";
|
||||||
import User from "discourse/models/user";
|
import User from "discourse/models/user";
|
||||||
import deprecated from "discourse-common/lib/deprecated";
|
import deprecated from "discourse-common/lib/deprecated";
|
||||||
import { getOwnerWithFallback } from "discourse-common/lib/get-owner";
|
import { getOwnerWithFallback } from "discourse-common/lib/get-owner";
|
||||||
|
import Topic from "./topic";
|
||||||
|
|
||||||
function extractByKey(collection, klass) {
|
function extractByKey(collection, klass) {
|
||||||
const retval = {};
|
const retval = {};
|
||||||
|
@ -190,6 +191,7 @@ export default class TopicList extends RestModel {
|
||||||
if (result) {
|
if (result) {
|
||||||
// the new topics loaded from the server
|
// the new topics loaded from the server
|
||||||
const newTopics = TopicList.topicsFrom(this.store, result);
|
const newTopics = TopicList.topicsFrom(this.store, result);
|
||||||
|
Topic.applyTransformations(newTopics);
|
||||||
|
|
||||||
this.forEachNew(newTopics, (t) => {
|
this.forEachNew(newTopics, (t) => {
|
||||||
t.set("highlight", topicsAdded++ === 0);
|
t.set("highlight", topicsAdded++ === 0);
|
||||||
|
|
|
@ -35,9 +35,10 @@ export function loadTopicView(topic, args) {
|
||||||
|
|
||||||
return PreloadStore.getAndRemove(`topic_${topic.id}`, () =>
|
return PreloadStore.getAndRemove(`topic_${topic.id}`, () =>
|
||||||
ajax(jsonUrl, { data })
|
ajax(jsonUrl, { data })
|
||||||
).then((json) => {
|
).then(async (json) => {
|
||||||
json.categories?.forEach((c) => topic.site.updateCategory(c));
|
json.categories?.forEach((c) => topic.site.updateCategory(c));
|
||||||
topic.updateFromJson(json);
|
topic.updateFromJson(json);
|
||||||
|
await Topic.applyTransformations([topic]);
|
||||||
return json;
|
return json;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { service } from "@ember/service";
|
||||||
import $ from "jquery";
|
import $ from "jquery";
|
||||||
import { Promise } from "rsvp";
|
import { Promise } from "rsvp";
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
|
import Bookmark from "discourse/models/bookmark";
|
||||||
import DiscourseRoute from "discourse/routes/discourse";
|
import DiscourseRoute from "discourse/routes/discourse";
|
||||||
import I18n from "discourse-i18n";
|
import I18n from "discourse-i18n";
|
||||||
|
|
||||||
|
@ -33,7 +34,7 @@ export default DiscourseRoute.extend({
|
||||||
controller.set("loading", true);
|
controller.set("loading", true);
|
||||||
|
|
||||||
return this._loadBookmarks(params)
|
return this._loadBookmarks(params)
|
||||||
.then((response) => {
|
.then(async (response) => {
|
||||||
if (!response.user_bookmark_list) {
|
if (!response.user_bookmark_list) {
|
||||||
return { bookmarks: [] };
|
return { bookmarks: [] };
|
||||||
}
|
}
|
||||||
|
@ -41,6 +42,7 @@ export default DiscourseRoute.extend({
|
||||||
const bookmarks = response.user_bookmark_list.bookmarks.map(
|
const bookmarks = response.user_bookmark_list.bookmarks.map(
|
||||||
controller.transform
|
controller.transform
|
||||||
);
|
);
|
||||||
|
await Bookmark.applyTransformations(bookmarks);
|
||||||
const loadMoreUrl = response.user_bookmark_list.more_bookmarks_url;
|
const loadMoreUrl = response.user_bookmark_list.more_bookmarks_url;
|
||||||
|
|
||||||
const model = { bookmarks, loadMoreUrl };
|
const model = { bookmarks, loadMoreUrl };
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { schedule } from "@ember/runloop";
|
import { next, schedule } from "@ember/runloop";
|
||||||
import Service, { service } from "@ember/service";
|
import Service, { service } from "@ember/service";
|
||||||
import { disableImplicitInjections } from "discourse/lib/implicit-injections";
|
import { disableImplicitInjections } from "discourse/lib/implicit-injections";
|
||||||
import { isTesting } from "discourse-common/config/environment";
|
import { isTesting } from "discourse-common/config/environment";
|
||||||
|
@ -43,9 +43,12 @@ export default class RouteScrollManager extends Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
const scrollLocation = this.historyStore.get(STORE_KEY) || [0, 0];
|
const scrollLocation = this.historyStore.get(STORE_KEY) || [0, 0];
|
||||||
schedule("afterRender", () => {
|
|
||||||
this.scrollElement.scrollTo(...scrollLocation);
|
next(() =>
|
||||||
});
|
schedule("afterRender", () =>
|
||||||
|
this.scrollElement.scrollTo(...scrollLocation)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#shouldScroll(routeInfo) {
|
#shouldScroll(routeInfo) {
|
||||||
|
|
|
@ -74,7 +74,7 @@ export default class StoreService extends Service {
|
||||||
const adapter = this.adapterFor(type);
|
const adapter = this.adapterFor(type);
|
||||||
|
|
||||||
let store = this;
|
let store = this;
|
||||||
return adapter.findAll(this, type, findArgs).then((result) => {
|
return adapter.findAll(this, type, findArgs).then(async (result) => {
|
||||||
let results = this._resultSet(type, result);
|
let results = this._resultSet(type, result);
|
||||||
if (adapter.afterFindAll) {
|
if (adapter.afterFindAll) {
|
||||||
results = adapter.afterFindAll(results, {
|
results = adapter.afterFindAll(results, {
|
||||||
|
@ -83,15 +83,21 @@ export default class StoreService extends Service {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
await adapter.applyTransformations?.([result]);
|
||||||
return results;
|
return results;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mostly for legacy, things like TopicList without ResultSets
|
// Mostly for legacy, things like TopicList without ResultSets
|
||||||
findFiltered(type, findArgs) {
|
findFiltered(type, findArgs) {
|
||||||
return this.adapterFor(type)
|
const adapter = this.adapterFor(type);
|
||||||
|
return adapter
|
||||||
.find(this, type, findArgs)
|
.find(this, type, findArgs)
|
||||||
.then((result) => this._build(type, result));
|
.then((result) => this._build(type, result))
|
||||||
|
.then(async (result) => {
|
||||||
|
await adapter.applyTransformations?.([result]);
|
||||||
|
return result;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_hydrateFindResults(result, type, findArgs) {
|
_hydrateFindResults(result, type, findArgs) {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { getOwner } from "@ember/application";
|
import { getOwner } from "@ember/application";
|
||||||
import { click, visit } from "@ember/test-helpers";
|
import { click, settled, visit } from "@ember/test-helpers";
|
||||||
import { test } from "qunit";
|
import { test } from "qunit";
|
||||||
import { setDefaultHomepage } from "discourse/lib/utilities";
|
import { setDefaultHomepage } from "discourse/lib/utilities";
|
||||||
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
|
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
|
||||||
|
@ -27,9 +27,11 @@ acceptance("Dynamic homepage handling", function () {
|
||||||
assertOnLatest("/");
|
assertOnLatest("/");
|
||||||
|
|
||||||
await router.transitionTo("/").followRedirects();
|
await router.transitionTo("/").followRedirects();
|
||||||
|
await settled();
|
||||||
assertOnLatest("/");
|
assertOnLatest("/");
|
||||||
|
|
||||||
await router.transitionTo("discovery.index").followRedirects();
|
await router.transitionTo("discovery.index").followRedirects();
|
||||||
|
await settled();
|
||||||
assertOnLatest("/");
|
assertOnLatest("/");
|
||||||
|
|
||||||
await click(".nav-item_latest a");
|
await click(".nav-item_latest a");
|
||||||
|
@ -66,9 +68,11 @@ acceptance("Dynamic homepage handling", function () {
|
||||||
assertOnCategories("/");
|
assertOnCategories("/");
|
||||||
|
|
||||||
await router.transitionTo("/").followRedirects();
|
await router.transitionTo("/").followRedirects();
|
||||||
|
await settled();
|
||||||
assertOnCategories("/");
|
assertOnCategories("/");
|
||||||
|
|
||||||
await router.transitionTo("discovery.index").followRedirects();
|
await router.transitionTo("discovery.index").followRedirects();
|
||||||
|
await settled();
|
||||||
assertOnCategories("/");
|
assertOnCategories("/");
|
||||||
|
|
||||||
await click(".nav-item_categories a");
|
await click(".nav-item_categories a");
|
||||||
|
|
Loading…
Reference in New Issue