From 78daae00cd6fef9b0338787cb69e0417e95d7827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20M=C3=BCller?= Date: Fri, 17 Jun 2016 10:11:16 +0200 Subject: [PATCH] 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 --- src/core/index.ts | 2 +- .../services/maps-api-loader/lazy-maps-api-loader.ts | 8 ++++++-- src/core/utils/browser-globals.ts | 7 +++++-- .../maps-api-loader/lazy-maps-api-loader.spec.ts | 10 ++++++---- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/core/index.ts b/src/core/index.ts index ab20929ce..8b7f840f4 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -11,6 +11,6 @@ export * from './services'; export * from './events'; export const GOOGLE_MAPS_PROVIDERS: any[] = [ - ...BROWSER_GLOBALS_PROVIDERS, + BROWSER_GLOBALS_PROVIDERS, provide(MapsAPILoader, {useClass: LazyMapsAPILoader}), ]; diff --git a/src/core/services/maps-api-loader/lazy-maps-api-loader.ts b/src/core/services/maps-api-loader/lazy-maps-api-loader.ts index cf5b6beba..a5208ce7b 100644 --- a/src/core/services/maps-api-loader/lazy-maps-api-loader.ts +++ b/src/core/services/maps-api-loader/lazy-maps-api-loader.ts @@ -1,4 +1,6 @@ -import {Injectable, Optional, Provider, provide} from '@angular/core'; +import {Inject, Injectable, Optional, Provider, provide} from '@angular/core'; + +import {DOCUMENT_GLOBAL, WINDOW_GLOBAL} from '../../utils/browser-globals'; import {MapsAPILoader} from './maps-api-loader'; @@ -95,7 +97,9 @@ export class LazyMapsAPILoader extends MapsAPILoader { private _window: Window; private _document: Document; - constructor(@Optional() config: LazyMapsAPILoaderConfig, w: Window, d: Document) { + constructor( + @Optional() config: LazyMapsAPILoaderConfig, @Inject(WINDOW_GLOBAL) w: Window, + @Inject(DOCUMENT_GLOBAL) d: Document) { super(); this._config = config || DEFAULT_CONFIGURATION; this._window = w; diff --git a/src/core/utils/browser-globals.ts b/src/core/utils/browser-globals.ts index 611cc0f65..0cc23e223 100644 --- a/src/core/utils/browser-globals.ts +++ b/src/core/utils/browser-globals.ts @@ -1,4 +1,7 @@ -import {Provider, provide} from '@angular/core'; +import {OpaqueToken, Provider, provide} from '@angular/core'; + +export const WINDOW_GLOBAL = new OpaqueToken('angular2-google-maps window_global'); +export const DOCUMENT_GLOBAL = new OpaqueToken('angular2-google-maps document_global'); export const BROWSER_GLOBALS_PROVIDERS: Provider[] = - [provide(Window, {useValue: window}), provide(Document, {useValue: document})]; + [provide(WINDOW_GLOBAL, {useValue: window}), provide(DOCUMENT_GLOBAL, {useValue: document})]; diff --git a/test/services/maps-api-loader/lazy-maps-api-loader.spec.ts b/test/services/maps-api-loader/lazy-maps-api-loader.spec.ts index 609372ce7..ae5c915d0 100644 --- a/test/services/maps-api-loader/lazy-maps-api-loader.spec.ts +++ b/test/services/maps-api-loader/lazy-maps-api-loader.spec.ts @@ -3,19 +3,21 @@ import {beforeEachProviders, describe, expect, inject, it} from '@angular/core/t import {LazyMapsAPILoader} from '../../../src/core/services/maps-api-loader/lazy-maps-api-loader'; import {MapsAPILoader} from '../../../src/core/services/maps-api-loader/maps-api-loader'; +import {DOCUMENT_GLOBAL, WINDOW_GLOBAL} from '../../../src/core/utils/browser-globals'; export function main() { describe('Service: LazyMapsAPILoader', () => { beforeEachProviders(() => { return [ - provide(MapsAPILoader, {useClass: LazyMapsAPILoader}), provide(Window, {useValue: {}}), - provide( - Document, {useValue: jasmine.createSpyObj('Document', ['createElement'])}) + provide(MapsAPILoader, {useClass: LazyMapsAPILoader}), + provide(WINDOW_GLOBAL, {useValue: {}}), provide(DOCUMENT_GLOBAL, { + useValue: jasmine.createSpyObj('Document', ['createElement']) + }) ]; }); it('should create the default script URL', - inject([MapsAPILoader, Document, Window], (loader: LazyMapsAPILoader, doc: Document) => { + inject([MapsAPILoader, DOCUMENT_GLOBAL], (loader: LazyMapsAPILoader, doc: Document) => { interface Script { src?: string; async?: boolean;