Giter Club home page Giter Club logo

jquery.batch's Introduction

jQuery.Batch

Provides a simple interface for sending batch Ajax requests with jQuery. Simply include the jquery.batch.js file after jQuery in your development environment.

Usage

The standard usage of jQuery.Batch consists of simply running Ajax requests inside an anonymous function within the batch constructor and calling the send method, like so:

$.batch(function () {
  $.get('/users?order=name');
  $.post('/users/1', { name: 'Joe Strummer' });
}).send();

Rather than sending the two requests individually, the data for each individual request would be serialized and sent as a single request to the server following the configuration settings described below.

Requests can also be added to a batch dynamically at any point by using the add method:

var batch = $.batch();

batch.add(function () {
  $.get('/users?order=name');
});

batch.send();

The send method itself can be passed a hash of options which are passed on to the $.ajax request for the entire batch. You could set a success callback to be handled upon completion of the full request, like so:

batch.send({ success: success });

In addition, jQuery.Batch will respect the beforeSend method of each individual request. If this function returns false, the request will not be added to the batch.

$.batch(function () {
  $.ajax({
    url: '/users?order=name',
    beforeSend: function (xhr, settings) {
      if (window.CachedRequests[settings.url]) {
        return false;
      }
    }
  });
}).send();

Configuration

By default, jQuery.Batch requests are sent with the following jQuery Ajax options set:

  • url: _bulk
  • type: POST
  • contentType: application/json
  • processData: false
  • dataType: text

All of these defaults (and any other jQuery Ajax options) can be changed using the $.batchSetup function, like so:

$.batchSetup({
  contentType: 'application/x-www-form-urlencoded',
  processData: true
});

In addition, the following helper functions are also configurable using $.batchSetup:

serialize([request], [xhr], [settings])

This is used to serialize the data of a single request. request is a hash consisting of:

  • method - the request method
  • path - the url path
  • query - a string of query parameters (if any were passed)
  • headers - a hash containing the request headers
  • body - the request body

By default, this simply returns the request hash unchanged.

toJSON([requests])

This is used to serialize an array of all requests before it is sent as a batch Ajax request. requests is simply an array of all the individual requests in a batch. By default, this returns the requests array after passing it through JSON.stringify.

parse([data])

This is used to parse out the response from the server. This must return an array of request hashes, with each hash consisting of (at least):

  • status - the response status code
  • body - the body of the request response

By default, parse assumes the server returns the results line-delimited (each request on one line) with each request as escaped-JSON text with a JSON body (thus, doubly-escaped JSON).

Testing Environment

Install Node.js. Make sure you've installed Grunt and Bower globally:

npm install grunt-cli -g
npm install bower -g

Then, install the development dependencies:

npm install

Lint and run the tests with:

npm test

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.