Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit 78daae0

Browse files
committed
fix(LazyMapsAPILoader): use OpaqueTokens for globs
The Interfaces Window and Document seem not to work in all enviroments, so we now use OpaqueTokens to make sure that it's working in all cases. Fixes #436 Closes #441
1 parent e7426c5 commit 78daae0

4 files changed

Lines changed: 18 additions & 9 deletions

File tree

src/core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ export * from './services';
1111
export * from './events';
1212

1313
export const GOOGLE_MAPS_PROVIDERS: any[] = [
14-
...BROWSER_GLOBALS_PROVIDERS,
14+
BROWSER_GLOBALS_PROVIDERS,
1515
provide(MapsAPILoader, {useClass: LazyMapsAPILoader}),
1616
];

src/core/services/maps-api-loader/lazy-maps-api-loader.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import {Injectable, Optional, Provider, provide} from '@angular/core';
1+
import {Inject, Injectable, Optional, Provider, provide} from '@angular/core';
2+
3+
import {DOCUMENT_GLOBAL, WINDOW_GLOBAL} from '../../utils/browser-globals';
24

35
import {MapsAPILoader} from './maps-api-loader';
46

@@ -95,7 +97,9 @@ export class LazyMapsAPILoader extends MapsAPILoader {
9597
private _window: Window;
9698
private _document: Document;
9799

98-
constructor(@Optional() config: LazyMapsAPILoaderConfig, w: Window, d: Document) {
100+
constructor(
101+
@Optional() config: LazyMapsAPILoaderConfig, @Inject(WINDOW_GLOBAL) w: Window,
102+
@Inject(DOCUMENT_GLOBAL) d: Document) {
99103
super();
100104
this._config = config || DEFAULT_CONFIGURATION;
101105
this._window = w;

src/core/utils/browser-globals.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import {Provider, provide} from '@angular/core';
1+
import {OpaqueToken, Provider, provide} from '@angular/core';
2+
3+
export const WINDOW_GLOBAL = new OpaqueToken('angular2-google-maps window_global');
4+
export const DOCUMENT_GLOBAL = new OpaqueToken('angular2-google-maps document_global');
25

36
export const BROWSER_GLOBALS_PROVIDERS: Provider[] =
4-
[provide(Window, {useValue: window}), provide(Document, {useValue: document})];
7+
[provide(WINDOW_GLOBAL, {useValue: window}), provide(DOCUMENT_GLOBAL, {useValue: document})];

test/services/maps-api-loader/lazy-maps-api-loader.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@ import {beforeEachProviders, describe, expect, inject, it} from '@angular/core/t
33

44
import {LazyMapsAPILoader} from '../../../src/core/services/maps-api-loader/lazy-maps-api-loader';
55
import {MapsAPILoader} from '../../../src/core/services/maps-api-loader/maps-api-loader';
6+
import {DOCUMENT_GLOBAL, WINDOW_GLOBAL} from '../../../src/core/utils/browser-globals';
67

78
export function main() {
89
describe('Service: LazyMapsAPILoader', () => {
910
beforeEachProviders(() => {
1011
return [
11-
provide(MapsAPILoader, {useClass: LazyMapsAPILoader}), provide(Window, {useValue: {}}),
12-
provide(
13-
Document, {useValue: jasmine.createSpyObj<Document>('Document', ['createElement'])})
12+
provide(MapsAPILoader, {useClass: LazyMapsAPILoader}),
13+
provide(WINDOW_GLOBAL, {useValue: {}}), provide(DOCUMENT_GLOBAL, {
14+
useValue: jasmine.createSpyObj<Document>('Document', ['createElement'])
15+
})
1416
];
1517
});
1618

1719
it('should create the default script URL',
18-
inject([MapsAPILoader, Document, Window], (loader: LazyMapsAPILoader, doc: Document) => {
20+
inject([MapsAPILoader, DOCUMENT_GLOBAL], (loader: LazyMapsAPILoader, doc: Document) => {
1921
interface Script {
2022
src?: string;
2123
async?: boolean;

0 commit comments

Comments
 (0)