You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts/correlated-objects-pattern.md
+8-6Lines changed: 8 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
title: Avoid using the context.sync method in loops
3
3
description: Learn how to use the split loop and correlated objects patterns to avoid calling context.sync in a loop.
4
4
ms.topic: best-practice
5
-
ms.date: 01/15/2025
5
+
ms.date: 01/31/2025
6
6
ms.localizationpriority: medium
7
7
---
8
8
@@ -133,7 +133,9 @@ The preceding example suggests the following procedure for turning a loop that c
133
133
134
134
Let's consider a more complex scenario where processing the items in the collection requires data that isn't in the items themselves. The scenario envisions a Word add-in that operates on documents created from a template with some boilerplate text. Scattered in the text are one or more instances of the following placeholder strings: "{Coordinator}", "{Deputy}", and "{Manager}". The add-in replaces each placeholder with some person's name. While the UI of the add-in isn't important to this article, the add-in could have a task pane with three text boxes, each labeled with one of the placeholders. The user enters a name in each text box and then presses a **Replace** button. The handler for the button creates an array that maps the names to the placeholders, and then replaces each placeholder with the assigned name.
135
135
136
-
You don't need to actually produce an add-in with this UI to experiment with the code. You can use the [Script Lab tool](../overview/explore-with-script-lab.md) to prototype the important code. Use the following assignment statement to create the mapping array.
136
+
You can use the [Script Lab tool](../overview/explore-with-script-lab.md) to follow along with the code snippets shown here. In Word, you can load the "Correlated objects pattern" sample or [import this sample code from the GitHub repo](https://raw.githubusercontent.com/OfficeDev/office-js-snippets/refs/heads/prod/samples/word/90-scenarios/correlated-objects-pattern.yaml).
137
+
138
+
The following assignment statement creates the mapping array between placeholder and assigned names.
137
139
138
140
```javascript
139
141
constjobMapping= [
@@ -143,14 +145,14 @@ const jobMapping = [
143
145
];
144
146
```
145
147
146
-
The following code shows how you might replace each placeholder with its assigned name if you used `context.sync` inside loops.
148
+
The following code shows how you might replace each placeholder with its assigned name if you used `context.sync` inside loops. This corresponds to the `replacePlaceholdersSlow` function in the sample.
147
149
148
150
```javascript
149
151
Word.run(async (context) => {
150
152
// The context.sync calls in the loops will degrade performance.
151
153
for (let i =0; i <jobMapping.length; i++) {
152
154
let options =Word.SearchOptions.newObject(context);
153
-
options.matchWildCards=false;
155
+
options.matchWildcards=false;
154
156
let searchResults =context.document.body.search(jobMapping[i].job, options);
In the preceding code, there's an outer and an inner loop. Each of them contains a `context.sync` call. Based on the first code snippet in this article, you probably see that the `context.sync` in the inner loop can simply be moved after the inner loop. But that would still leave the code with a `context.sync` (two of them actually) in the outer loop. The following code shows how you can remove `context.sync` from the loops. We discuss the code later.
170
+
In the preceding code, there's an outer and an inner loop. Each of them contains a `context.sync` call. Based on the first code snippet in this article, you probably see that the `context.sync` in the inner loop can simply be moved after the inner loop. But that would still leave the code with a `context.sync` (two of them actually) in the outer loop. The following code shows how you can remove `context.sync` from the loops. It corresponds to the `replacePlaceholders` function in the sample. We discuss the code later.
169
171
170
172
```javascript
171
173
Word.run(async (context) => {
172
174
173
175
constallSearchResults= [];
174
176
for (let i =0; i <jobMapping.length; i++) {
175
177
let options =Word.SearchOptions.newObject(context);
176
-
options.matchWildCards=false;
178
+
options.matchWildcards=false;
177
179
let searchResults =context.document.body.search(jobMapping[i].job, options);
0 commit comments