From 2ca4236201aecf45e2c52bcad2c17c0cfe7c3f50 Mon Sep 17 00:00:00 2001 From: Paul Taylor Date: Tue, 29 Dec 2015 17:31:11 -0800 Subject: [PATCH] feat(ajax): adds ajax methods from rx-dom. --- package.json | 1 + src/Observable.ts | 5 +++++ src/add/observable/dom/get.ts | 5 +++++ src/add/observable/dom/getJSON.ts | 5 +++++ src/add/observable/dom/post.ts | 5 +++++ src/observable/dom/get.ts | 12 ++++++++++++ src/observable/dom/getJSON.ts | 12 ++++++++++++ src/observable/dom/post.ts | 13 +++++++++++++ 8 files changed, 58 insertions(+) create mode 100644 src/add/observable/dom/get.ts create mode 100644 src/add/observable/dom/getJSON.ts create mode 100644 src/add/observable/dom/post.ts create mode 100644 src/observable/dom/get.ts create mode 100644 src/observable/dom/getJSON.ts create mode 100644 src/observable/dom/post.ts diff --git a/package.json b/package.json index d878ca759e..b1a0536748 100644 --- a/package.json +++ b/package.json @@ -107,6 +107,7 @@ "http-server": "0.8.0", "istanbul": "0.3.22", "jasmine": "2.4.1", + "jasmine-ajax": "^3.2.0", "jasmine-core": "2.4.1", "karma": "0.13.15", "karma-browserify": "4.4.2", diff --git a/src/Observable.ts b/src/Observable.ts index 2b6ffc5b59..a7bec2a5c6 100644 --- a/src/Observable.ts +++ b/src/Observable.ts @@ -161,6 +161,11 @@ export class Observable implements CoreOperators { return this.source.subscribe(subscriber); } + static get: (url: string | any) => Observable; + static post: (url: string | any, body?: any) => Observable; + static ajax: (options: string | any) => Observable; + static getJSON: (url: string | any) => Observable; + // static method stubs static ajax: AjaxCreationMethod; static bindCallback: typeof BoundCallbackObservable.create; diff --git a/src/add/observable/dom/get.ts b/src/add/observable/dom/get.ts new file mode 100644 index 0000000000..f3b808358a --- /dev/null +++ b/src/add/observable/dom/get.ts @@ -0,0 +1,5 @@ +import {Observable} from '../../../Observable'; +import {get} from '../../../observable/dom/get'; +Observable.get = get; + +export var _void: void; diff --git a/src/add/observable/dom/getJSON.ts b/src/add/observable/dom/getJSON.ts new file mode 100644 index 0000000000..7ba03ee739 --- /dev/null +++ b/src/add/observable/dom/getJSON.ts @@ -0,0 +1,5 @@ +import {Observable} from '../../../Observable'; +import {getJSON} from '../../../observable/dom/getJSON'; +Observable.getJSON = getJSON; + +export var _void: void; diff --git a/src/add/observable/dom/post.ts b/src/add/observable/dom/post.ts new file mode 100644 index 0000000000..53265a4053 --- /dev/null +++ b/src/add/observable/dom/post.ts @@ -0,0 +1,5 @@ +import {Observable} from '../../../Observable'; +import {post} from '../../../observable/dom/post'; +Observable.post = post; + +export var _void: void; diff --git a/src/observable/dom/get.ts b/src/observable/dom/get.ts new file mode 100644 index 0000000000..e70a274ebd --- /dev/null +++ b/src/observable/dom/get.ts @@ -0,0 +1,12 @@ +import {Observable} from '../../Observable'; +import {AjaxObservable} from './ajax'; + +function get(url: string | any): Observable { + if (typeof url === 'string') { + return new AjaxObservable({ url }); + } else { + return new AjaxObservable(url); + } +} + +export { AjaxObservable, get }; diff --git a/src/observable/dom/getJSON.ts b/src/observable/dom/getJSON.ts new file mode 100644 index 0000000000..332c6798d7 --- /dev/null +++ b/src/observable/dom/getJSON.ts @@ -0,0 +1,12 @@ +import {root} from '../../util/root'; +import {Observable} from '../../Observable'; +import {AjaxObservable} from './ajax'; + +function getJSON(url: string | any): Observable { + if (!root.JSON && typeof root.JSON.parse !== 'function') { + throw new TypeError('JSON is not supported in your runtime.'); + } + return new AjaxObservable({ url, emitType: 'json', responseType: 'json' }); +} + +export { AjaxObservable, getJSON }; diff --git a/src/observable/dom/post.ts b/src/observable/dom/post.ts new file mode 100644 index 0000000000..25f1b40696 --- /dev/null +++ b/src/observable/dom/post.ts @@ -0,0 +1,13 @@ +import {Observable} from '../../Observable'; +import {AjaxObservable} from './ajax'; + +function post(url: string | any, body?: any): Observable { + if (typeof url === 'string') { + return new AjaxObservable({ url, body, method: 'POST' }); + } else { + ( url).method = 'POST'; + return new AjaxObservable(( url)); + } +} + +export { AjaxObservable, post };