rx ajax,GitHub - Carrooi/Js-RxAjax: Simple ajax reactive library

68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f72782d616a61782e7376673f7374796c653d666c61742d737175617265

68747470733a2f2f696d672e736869656c64732e696f2f67656d6e617369756d2f436172726f6f692f4a732d5278416a61782e7376673f7374796c653d666c61742d737175617265

68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f436172726f6f692f4a732d5278416a61782e7376673f7374796c653d666c61742d737175617265

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f6e6174652d50617950616c2d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265

Rx-Ajax

Super simple (but advanced) ajax reactive library. Using RxJS.

Supports sending files.

Installation

$ npm install rx-ajax

Usage

import {Http} from 'rx-ajax';

let http = new Http;

let data = {number: 5};

let options = {};

http.request('/users', 'GET', data, options).subscribe((response) => {

console.log(response.json());

});

Shortcuts

http.get('/api', data, options).subscribe((response) => {});

http.post('/api', data, options).subscribe((response) => {});

http.files('/api', filesList, data, options).subscribe((response) => {});

Options

jsonp: name of callback for jsonp requests, when true is given callback name is used. Default is false

jsonPrefix: prefix for json requests. Read more here. Default is null

mimeType: Default is null

headers: List of headers to be send to server. Default is {}

files: List of files to be send to server. Default is []

Queue

By default all your requests are called from queue one by one, so there is always just one request running (or zero).

Inspiration is from this article http://blog.alexmaccaw.com/queuing-ajax-requests.

You can disable this feature with ImmediateQueue:

import {Http} from 'rx-ajax';

import {ImmediateQueue} from 'rx-ajax/queue';

let http = new Http({

queue: new ImmediateQueue

});

Events

http.send.subscribe(function(response, request) {

console.log('In any moment, new http request will be send to server');

});

http.afterSend.subscribe(function(response, request) {

console.log('I just sent some request to server, but there is still no response');

});

http.success.subscribe(function(response, request) {

console.log('I have got response from server without any error :-)');

});

http.error.subscribe(function(err, response, request) {

console.log('Sorry, there was some error with this response');

});

Extensions

Sometimes it will be better to register whole group of events and this group is called extension.

import {AbstractExtension} from 'rx-ajax/extensions';

import {Request, Response} from 'rx-ajax';

class CustomExtension extends AbstractExtension

{

public send(request: Request): void

{

// todo

}

public afterSend(request: Request): void

{

// todo

}

public success(response: Response): void

{

// todo

}

public error(err: Error): void

{

// todo

}

}

// ...

http.addExtension(new CustomExtension);

Built in extensions

Links

This extension listen for all click events on A tags with ajax class and call them via ajax.

import {Links} from 'rx-ajax/extensions';

http.addExtension(new Links);

Loading

import {Loading} from 'rx-ajax/extensions';

http.addExtension(new Loading);

Offline

This extension tests if your favicon.ico is accessible. You can change test destination by specifying Offline's

constructor argument.

import {Offline} from 'rx-ajax/extensions';

http.addExtension(new Offline);

http.connected.subscribe(function() {

alert("We're online again :-)");

});

http.disconnected.subscribe(function() {

alert('Lost internet connection');

});

Redirect

If your server sends json data with redirect variable, then you will be redirected to address in this variable.

import {Redirect} from 'rx-ajax/extensions';

http.addExtension(new Redirect);

Snippets

If there is snippets object in response data with html id and content pairs, then this extension will iterate through

this object, find element in page with given id and change content of given element into the one from response data.

import {Snippets} from 'rx-ajax/extensions';

http.addExtension(new Snippets);

Known limitations

All non ASCII chars (eg. letters with diacritics) in file names are converted to ASCII chars before uploading.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值