Skip to content

Commit

Permalink
Cleaned up Ajax a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacwright committed Jul 27, 2010
1 parent 1916d37 commit 090ce31
Showing 1 changed file with 47 additions and 57 deletions.
104 changes: 47 additions & 57 deletions src/ajax.js
Original file line number Original file line Diff line number Diff line change
@@ -1,79 +1,56 @@


var Ajax = new Class({ var Ajax = {
extend: EventDispatcher, defaults: {

method: 'get',
constructor: function() { async: true,
this.headers = {}; format: function(data) { return data; }
},

setRequestHeader: function(name, value) {
this.headers[name] = value;
}, },


send: function(method, url, user, password) { send: function(options) {
extend(options, Ajax.defaults);

var xhr = new XMLHttpRequest(), response = new Response(); var xhr = new XMLHttpRequest(), response = new Response();
response.xhr = xhr; response.xhr = xhr;


if (user) { if (options.user) {
xhr.open(method, url, true, user, password); xhr.open(options.method, options.url, options.async, options.user, options.password);
} else { } else {
xhr.open(method, url); xhr.open(options.method, options.url, options.async);
} }
for (var i in this.headers) {
if (!this.headers.hasOwnProperty(i)) continue; if (options.headers) {
xhr.setRequestHeader(i, this.headers[i]); for (var i in options.headers) {
if (!options.headers.hasOwnProperty(i)) continue;
xhr.setRequestHeader(i, options.headers[i]);
}
} }

if (options.progress) response.on('progress', options.complete);
if (options.complete) response.on('complete', options.complete);
if (options.error) response.on('error', options.error);


var lastIndex = 0, results, ajax = this; var lastIndex = 0, results;
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if (xhr.readyState == 3) { if (xhr.readyState == 3) {
try { try {
results = ajax.format(xhr.responseText.substring(lastIndex)); results = options.format(xhr.responseText.substring(lastIndex));
lastIndex = xhr.responseText.length; lastIndex = xhr.responseText.length;
} catch(e) {} } catch(e) {}
ajax.dispatchEvent(new DataEvent('progress', results));
response.trigger('progress', results); response.trigger('progress', results);
} else if (xhr.readyState == 4) { } else if (xhr.readyState == 4) {
try { try {
results = ajax.format(xhr.responseText); results = options.format(xhr.responseText);
} catch(e) { } catch(e) {
// formating error // formating error
alert(e); alert(e);
} }
ajax.dispatchEvent(new DataEvent('complete', results));
response.trigger('complete', results); response.trigger('complete', results);
} }
} }
xhr.send(); xhr.send();
return response; return response;
},

format: function(data) {
if (data == '' || data == null) return null;
else return JSON.parse(data);
},

get: function() {

},

post: function() {

},

put: function() {

} }
// };
// var xhr = new XMLHttpRequest();
// xhr.open('get', '/rest/engagement/posts');
// xhr.setRequestHeader('Accept', 'application/json');
// xhr.onreadystatechange = function() {
// if (xhr.readyState != 4) return;
// document.find('#articles').data = JSON.parse(xhr.responseText);
// }
// xhr.send();
});


/** /**
* Represents a single service which can be * Represents a single service which can be
Expand All @@ -85,12 +62,13 @@ var AjaxService = new Component({
events: ['progress', 'complete', 'error'], events: ['progress', 'complete', 'error'],


constructor: function() { constructor: function() {
var ajax = this.ajax = new Ajax(), service = this; var ajax = this.ajax = new AjaxEndpoint(), service = this;
this.headers = {};
this.prefix = ''; this.prefix = '';


this.findAll('headers header').forEach(function(header) { this.findAll('headers header').forEach(function(header) {
if (!header.hasAttribute('name') || !header.hasAttribute('value')) return; if (!header.hasAttribute('name') || !header.hasAttribute('value')) return;
ajax.setRequestHeader(header.getAttribute('name'), header.getAttribute('value')); this.headers[header.getAttribute('name')] = header.getAttribute('value');
}); });


this.calls = this.findAll('call').forEach(function(call) { this.calls = this.findAll('call').forEach(function(call) {
Expand All @@ -99,7 +77,19 @@ var AjaxService = new Component({
}, },


send: function(method, url) { send: function(method, url) {
return this.ajax.send(method, this.prefix + url, this.user, this.password); return Ajax.send({
method: method,
url: this.prefix + url,
format: this.format,
headers: this.headers,
user: this.user,
password: this.password
});
},

format: function(data) {
if (data == '' || data == null) return null;
else return JSON.parse(data);
} }
}); });


Expand Down Expand Up @@ -154,7 +144,7 @@ var Response = new Class({


constructor: function() { constructor: function() {
this.status = 'progress'; this.status = 'progress';
this.handlers = {complete: [], fault: [], progress: []}; this.handlers = {complete: [], error: [], progress: []};
}, },


on: function(type, handler) { on: function(type, handler) {
Expand All @@ -179,8 +169,8 @@ var Response = new Class({
this.trigger('complete', data); this.trigger('complete', data);
}, },


triggerFault: function(error) { triggerError: function(error) {
this.trigger('fault', error); this.trigger('error', error);
}, },


trigger: function(type, data) { trigger: function(type, data) {
Expand All @@ -196,12 +186,12 @@ var Response = new Class({
if (result !== undefined) { if (result !== undefined) {
if (result instanceof Response) { if (result instanceof Response) {
result.on('complete', this.triggerComplete.boundTo(this)); result.on('complete', this.triggerComplete.boundTo(this));
result.on('fault', this.triggerFault.boundTo(this)); result.on('error', this.triggerError.boundTo(this));
return; // pick back up after this response is done return; // pick back up after this response is done
} else { } else {
if (result instanceof Error && this.status == 'complete') { if (result instanceof Error && this.status == 'complete') {
this.status = 'fault'; this.status = 'error';
handlers = this.handlers.fault; handlers = this.handlers.error;
} }
data = result; data = result;
} }
Expand Down

0 comments on commit 090ce31

Please sign in to comment.