Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Commit 89afc4f

Browse files
Mark change detector for check after a resource string is loaded asynchronously (#243)
* Mark change detector for check after a resource string is loaded asynchronously blackbaud/skyux2#933 * Removed temporary code used during local testing
1 parent 837b0dd commit 89afc4f

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

runtime/i18n/resources.pipe.spec.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import { SkyAppResourcesPipe } from '@blackbaud/skyux-builder/runtime/i18n/resou
55

66
describe('Resources pipe', () => {
77
let resources: SkyAppResourcesService;
8+
let changeDetector: any;
89

910
beforeEach(() => {
11+
changeDetector = {
12+
markForCheck: jasmine.createSpy('markForCheck')
13+
};
14+
1015
resources = {
1116
getString: (name: string, ...args) => {
1217
let value: string;
@@ -23,19 +28,19 @@ describe('Resources pipe', () => {
2328
});
2429

2530
it('should return the expected string', () => {
26-
let pipe = new SkyAppResourcesPipe(resources);
31+
let pipe = new SkyAppResourcesPipe(changeDetector, resources);
2732

2833
expect(pipe.transform('hi')).toBe('hello');
2934
});
3035

3136
it('should return the expected string formatted with the specified parameters', () => {
32-
let pipe = new SkyAppResourcesPipe(resources);
37+
let pipe = new SkyAppResourcesPipe(changeDetector, resources);
3338

3439
expect(pipe.transform('hi', 'abc')).toBe('format me abc');
3540
});
3641

3742
it('should cache strings that have been retrieved via the resource service', () => {
38-
let pipe = new SkyAppResourcesPipe(resources);
43+
let pipe = new SkyAppResourcesPipe(changeDetector, resources);
3944

4045
const getStringSpy = spyOn(resources, 'getString').and.callThrough();
4146

@@ -47,7 +52,7 @@ describe('Resources pipe', () => {
4752
});
4853

4954
it('should consider format args as part of the cache key', () => {
50-
let pipe = new SkyAppResourcesPipe(resources);
55+
let pipe = new SkyAppResourcesPipe(changeDetector, resources);
5156

5257
const getStringSpy = spyOn(resources, 'getString').and.callThrough();
5358

@@ -61,4 +66,14 @@ describe('Resources pipe', () => {
6166
expect(getStringSpy).toHaveBeenCalledTimes(2);
6267
});
6368

69+
it('should mark the change detector for check when the string is loaded asynchronously', () => {
70+
let pipe = new SkyAppResourcesPipe(changeDetector, resources);
71+
72+
pipe.transform('hi');
73+
pipe.transform('hi');
74+
pipe.transform('hi');
75+
76+
expect(changeDetector.markForCheck).toHaveBeenCalledTimes(1);
77+
});
78+
6479
});

runtime/i18n/resources.pipe.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import {
2+
ChangeDetectorRef,
3+
Pipe,
4+
PipeTransform
5+
} from '@angular/core';
6+
17
import { SkyAppResourcesService } from './resources.service';
2-
import { Pipe, PipeTransform } from '@angular/core';
38

49
/**
510
* An Angular pipe for displaying a resource string.
@@ -12,6 +17,7 @@ export class SkyAppResourcesPipe implements PipeTransform {
1217
private resourceCache: {[key: string]: any} = {};
1318

1419
constructor(
20+
private changeDetector: ChangeDetectorRef,
1521
private resourcesSvc: SkyAppResourcesService
1622
) { }
1723

@@ -25,7 +31,10 @@ export class SkyAppResourcesPipe implements PipeTransform {
2531
if (!(cacheKey in this.resourceCache)) {
2632
this.resourcesSvc
2733
.getString(name, ...args)
28-
.subscribe(result => this.resourceCache[cacheKey] = result);
34+
.subscribe((result) => {
35+
this.resourceCache[cacheKey] = result;
36+
this.changeDetector.markForCheck();
37+
});
2938
}
3039

3140
return this.resourceCache[cacheKey];

0 commit comments

Comments
 (0)