From a0353477d5bc1d99c32922dac4f67d14367e2981 Mon Sep 17 00:00:00 2001 From: darlin Date: Fri, 3 Feb 2017 18:08:49 +0800 Subject: [PATCH 1/2] fix(http):fix jsonp callbackname is undefined --- .../http/src/backends/browser_jsonp.ts | 20 +++++++------------ modules/playground/src/jsonp/people.json | 2 +- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/modules/@angular/http/src/backends/browser_jsonp.ts b/modules/@angular/http/src/backends/browser_jsonp.ts index 022431c13678..8deeb98b22d5 100644 --- a/modules/@angular/http/src/backends/browser_jsonp.ts +++ b/modules/@angular/http/src/backends/browser_jsonp.ts @@ -12,12 +12,10 @@ let _nextRequestId = 0; export const JSONP_HOME = '__ng_jsonp__'; let _jsonpConnections: {[key: string]: any} = null; -function _getJsonpConnections(): {[key: string]: any} { - const w: {[key: string]: any} = typeof window == 'object' ? window : {}; - if (_jsonpConnections === null) { - _jsonpConnections = w[JSONP_HOME] = {}; - } - return _jsonpConnections; +const _global: {[key: string]: any} = typeof window == 'object' ? window : {}; + +function _getJsonpCallbackName(id: string): string { + return `${JSONP_HOME}${id}_finished`; } // Make sure not to evaluate this in a non-browser environment! @@ -32,17 +30,13 @@ export class BrowserJsonp { nextRequestID(): string { return `__req${_nextRequestId++}`; } - requestCallback(id: string): string { return `${JSONP_HOME}${id}_finished`; } + requestCallback(id: string): string { return _getJsonpCallbackName(id); } exposeConnection(id: string, connection: any): void { - const connections = _getJsonpConnections(); - connections[id] = connection; + _global[_getJsonpCallbackName(id)] = connection.finished.bind(connection); } - removeConnection(id: string): void { - const connections = _getJsonpConnections(); - connections[id] = null; - } + removeConnection(id: string): void { _global[_getJsonpCallbackName(id)] = null; } // Attach the