UNPKG

ng2-http-loader

Version:

## ng2-http-loader [![Angular 2 Style Guide](https://mgechev.github.io/angular2-style-guide/images/badge.svg)](https://github.com/mgechev/angular2-style-guide)

109 lines (108 loc) 4.41 kB
"use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var core_1 = require('@angular/core'); var http_1 = require('@angular/http'); var rxjs_1 = require('rxjs'); require('rxjs/add/operator/map'); require('rxjs/add/operator/catch'); var ng2_http_loader_emitter_1 = require('./ng2-http-loader-emitter'); var Ng2Http = (function (_super) { __extends(Ng2Http, _super); function Ng2Http(backend, defaultOptions, injector) { _super.call(this, backend, defaultOptions); this.injector = injector; this.totalRequests = 0; this.emitter = injector.get(ng2_http_loader_emitter_1.Ng2HttpLoaderEmitter); } Ng2Http.prototype.request = function (url, options) { return _super.prototype.request.call(this, url, options); }; Ng2Http.prototype.get = function (url, options) { var _this = this; return rxjs_1.Observable.create(function (observer) { _this.onRequest(); _super.prototype.get.call(_this, (url), options) .subscribe(function (res) { observer.next(res); observer.complete(); }); }); }; Ng2Http.prototype.post = function (url, body, options) { var _this = this; return rxjs_1.Observable.create(function (observer) { _this.onRequest(); _super.prototype.post.call(_this, url, body, options).map(function (res) { _this.onRequestEnd(); return res; }).subscribe(function (res) { observer.next(res); observer.complete(); }); }); }; Ng2Http.prototype.put = function (url, body, options) { var _this = this; return rxjs_1.Observable.create(function (observer) { _this.onRequest(); _super.prototype.put.call(_this, url, body, options).map(function (res) { _this.onRequestEnd(); return res; }).subscribe(function (res) { observer.next(res); observer.complete(); }); }); }; Ng2Http.prototype.delete = function (url, options) { var _this = this; return rxjs_1.Observable.create(function (observer) { _this.onRequest(); _super.prototype.delete.call(_this, url, options).map(function (res) { _this.onRequestEnd(); return res; }).subscribe(function (res) { observer.next(res); observer.complete(); }); }); }; Ng2Http.prototype.onRequest = function () { this.totalRequests++; this.emitter.emit({ action: 'init', count: this.totalRequests }); }; Ng2Http.prototype.onRequestEnd = function () { this.totalRequests--; this.emitter.emit({ action: 'end', count: this.totalRequests }); }; Ng2Http = __decorate([ core_1.Injectable(), __metadata('design:paramtypes', [http_1.ConnectionBackend, http_1.RequestOptions, core_1.Injector]) ], Ng2Http); return Ng2Http; }(http_1.Http)); exports.Ng2Http = Ng2Http; // This is to make happy the AoT compiler function httpLoaderFactory(xhrBackend, requestOptions, injector) { return new Ng2Http(xhrBackend, requestOptions, injector); } exports.httpLoaderFactory = httpLoaderFactory;