Skip to content

Commit f43b8f1

Browse files
authored
Merge pull request #6 from episerver/user/base/simplifying-saved-subscribe
Simplify subscribing to changes
2 parents 7ea2707 + 50dd647 commit f43b8f1

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,23 @@ You need to subscribe to a special event in order to know once content has been
324324
In this repo the subscription is already done in [onContentSaved.ts](src%2Fhelpers%2FonContentSaved.ts)
325325

326326
```ts
327-
epi.subscribe("contentSaved", function (message: ContentSavedEventArgs) {
328-
// your code here
327+
window.addEventListener("optimizely:cms:contentSaved", (event: any) => {
328+
const message = event.detail as ContentSavedEventArgs;
329329
});
330330
```
331331

332+
where is defined as following:
333+
334+
```ts
335+
interface ContentSavedEventArgs {
336+
contentLink: string;
337+
previewUrl: string;
338+
isIndexed: boolean;
339+
properties: PropertySaved[];
340+
parentId?: string;
341+
sectionId?: string;
342+
}
343+
```
344+
332345
More details here:
333346
https://docs.developers.optimizely.com/content-management-system/v1.0.0-CMS-SaaS/docs/enable-live-preview#refresh-the-applications-view-when-content-has-changed

src/helpers/onContentSaved.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,12 @@ function isInEditMode() {
2626
return !!getPreviewToken();
2727
}
2828

29-
function ensureEpiLoaded() {
30-
if (typeof window !== "undefined") {
31-
const epi = (window as any).epi;
32-
if (typeof epi !== "undefined" && epi.isEditable && epi.ready) {
33-
return epi;
34-
}
35-
} else {
36-
return null;
37-
}
38-
}
39-
4029
export function onContentSaved(callback: ((message: ContentSavedEventArgs) => void )) {
4130
if (!isInEditMode()) {
4231
return;
4332
}
44-
45-
const epi = ensureEpiLoaded();
46-
if (epi) {
47-
console.info("successfully connected to CMS.");
48-
epi.subscribe("contentSaved", function (message: ContentSavedEventArgs) {
49-
callback(message);
50-
});
51-
} else {
52-
setTimeout(() => {
53-
console.info("connecting to CMS...");
54-
onContentSaved(callback);
55-
}, 100);
56-
}
33+
34+
window.addEventListener("optimizely:cms:contentSaved", (event: any) => {
35+
callback(event.detail);
36+
});
5737
}

0 commit comments

Comments
 (0)