Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions modules/@angular/http/src/backends/browser_jsonp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert


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 <script> element to the DOM
send(node: any): void { document.body.appendChild(<Node>(node)); }
Expand Down
5 changes: 3 additions & 2 deletions modules/@angular/http/test/backends/jsonp_backend_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ export function main() {
expect(instance).toBeAnInstanceOf(JSONPConnection);
});

it('callback name should not contain dots', () => {
it('callback name should be a function name', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we saw testing callback name doesn't actually verify anything.
Need a test for Uncaught ReferenceError: __ng_jsonp____req0_finished is not defined arror

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this callback name can be any validate function name, and I think it's enough. What to cause __ng_jsonp____req0_finished is not defined is the error of the data https://github.com/angular/angular/blob/master/modules/playground/src/jsonp/people.json#L2

const funcNameReg = /^[$A-Za-z_][$A-Za-z_\d]*$/i;
const domJsonp = new MockBrowserJsonp();
const callback: string = domJsonp.requestCallback(domJsonp.nextRequestID());
expect(callback.indexOf('.') === -1).toBeTruthy();
expect(funcNameReg.test(callback)).toBeTruthy();
});

describe('JSONPConnection', () => {
Expand Down
2 changes: 1 addition & 1 deletion modules/playground/src/jsonp/people.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This can only be requested once due to constant method name :(
__ng_jsonp__.__req0.finished([{"name":"caitp"}])
__ng_jsonp____req0_finished([{"name":"caitp"}])