Skip to content

Commit 212d65a

Browse files
committed
DOC-3246: Updates to fetch_users partial
1 parent b4185ec commit 212d65a

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed
Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[[fetch_users]]
22
== `fetch_users`
33

4-
A **required callback function** that fetches user data. This function is called with an array of user IDs and should return a `Promise` that resolves to an array of user objects. The callback is used by the xref:userlookup.adoc[`UserLookup`] API.
4+
A **required callback function** that fetches user data. This function is called with an array of user IDs and should return a `Promise` that resolves to an array of user objects. The callback is used by the xref:userlookup.adoc[`UserLookup`] API. If the returned array does not include all requested user IDs, promises for the missing users will be rejected with a "User {id} not found" error.
55

66
*Type:* `Function`
77

@@ -15,25 +15,43 @@ A **required callback function** that fetches user data. This function is called
1515

1616
[source,javascript]
1717
----
18-
const userDirectory = {
19-
'janedoe': { id: 'janedoe', name: 'Jane Doe', avatar: 'https://example.com/avatar/jane.png' },
20-
'johnsmith': { id: 'johnsmith', name: 'John Smith' },
21-
'alextaylor': { id: 'alextaylor', name: 'Alex Taylor', avatar: 'https://example.com/avatar/alex.png' }
22-
};
23-
2418
tinymce.init({
2519
selector: '#editor',
2620
user_id: 'alextaylor',
27-
fetch_users: (userIds) => {
28-
const results = userIds.map((id) => {
29-
const user = userDirectory[id];
30-
if (user) {
31-
return user;
32-
} else {
33-
throw new Error(`User ${id} not found`);
34-
}
35-
});
36-
return Promise.resolve(results);
37-
}
21+
fetch_users: (userIds) => Promise.all(userIds
22+
.map((userId) =>
23+
fetch(`/users/${userId}`) // Fetch user data from the server
24+
.then((response) => response.json())
25+
.catch(() => ({ id: userId })) // Still return a valid user object even if the fetch fails
26+
)),
27+
});
28+
----
29+
30+
=== Example: returning user array with validation
31+
32+
[source,javascript]
33+
----
34+
tinymce.init({
35+
selector: '#editor',
36+
user_id: 'alextaylor',
37+
fetch_users: async (userIds) => {
38+
const users = await fetch('/users', {
39+
method: 'POST',
40+
headers: { 'Content-Type': 'application/json' },
41+
body: JSON.stringify({ userIds })
42+
})
43+
.then((response) => response.json())
44+
.catch(() =>
45+
userIds.map((userId) =>
46+
({ id: userId }) // Still returns valid users even if the fetch fails
47+
)
48+
);
49+
50+
return userIds.map(
51+
(userId) =>
52+
users.find((user) => user.id === userId)
53+
|| ({ id: userId }) // Still returns a valid user even if it wasn't returned from the server
54+
)
55+
}
3856
});
3957
----

0 commit comments

Comments
 (0)