Sixtest Wiki:API5/Ajax

(function ajax {	"use strict";	$API("Project:API5/Ajax", function (ajax) { // TODO: major cleanup, combine some ajax calls (.get, .getToken and .post requests are almost the same) var settings = {}; settings.paused = false; settings.maxConnections = 3; ajax.extend({			data: {},			resume: function {				if (settings.paused) {					ajax.run;				}			},			pause: function (state) {				settings.paused = state !== undefined ? state : !settings.paused;				ajax.resume;				return ajax;			},			get: function (pagenames, callback) {				// formats for ajax.exec;				var reqs = this.clone, i = 0, len = pagenames.length, getRequest = [];				reqs.data.action = 'raw';				for (i; i < len; i += 1) {					getRequest[i] = {						url: pagenames[i],						type: this.type || 'GET',						data: {							'action': this.data.action || 'raw',							'format': this.data.format || 'json'						}					};				}				reqs.append(getRequest);				reqs.callback = callback;				reqs.exec;			},			post: function (pagenames, texts, callback) {				var reqs = ajax.clone, i = 0, len = pagenames.length, postRequest = [];				for (i; i < len; i += 1) {					postRequest[i] = {						url: this.url || ajax.url, type: this.type || 'POST', data : { 'action': this.data.action || 'edit', 'title': pagenames[i], 'format': this.data.format || 'json', 'text': texts[i], // TODO: create User extension 'token': user.edittoken, 'minor': this.data.minor || 'minor', 'summary': this.data.summary || 'JS edit test.' }					};				}				reqs.append(postRequest); reqs.exec; },			exec: function { var i = 0, len = this.length, done = 0, responses = [], callback = this.callback, cb_success, cb_error; console.log(callback); cb_success = function (i) { return function (response) { done += 1; responses[i] = response; if (done === len && callback) { callback(responses); }					};				};				cb_error = function (i) { return function { done += 1; responses[i] = null; if (done === len && callback) { callback(responses); }					};				};				for (i; i < len; i += 1) { this[i].success = cb_success(i); this[i].error = cb_error(i); $.ajax(this[i]); this[i] = undefined; }			}		});	}); });