Skip to content

Commit 667ec4c

Browse files
committed
Ajax support
1 parent 78dd353 commit 667ec4c

File tree

4 files changed

+94
-23
lines changed

4 files changed

+94
-23
lines changed

assets/js/ao.js

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var Ao = (function () {
22
// Helpers
3+
function doNothing() { }
4+
35
function doOne(callback) {
46
callback.call(this, this.e, 0);
57
return this;
@@ -36,21 +38,17 @@
3638
this._data = {};
3739
if (typeof elemOrId === 'undefined') {
3840
single(this, empty);
39-
}
40-
else if (typeof elemOrId === 'string') {
41+
} else if (typeof elemOrId === 'string') {
4142
single(this, ao.get(elemOrId));
42-
}
43-
else {
43+
} else {
4444
if (isArrayLike(elemOrId)) {
4545
if (elemOrId.length === 1) {
4646
single(this, elemOrId[0]);
47-
}
48-
else {
47+
} else {
4948
this.e = elemOrId;
5049
this._do = doAll;
5150
}
52-
}
53-
else {
51+
} else {
5452
single(this, elemOrId);
5553
}
5654
}
@@ -294,6 +292,79 @@
294292

295293
ao.isArrayLike = isArrayLike;
296294

295+
ao.merge = function (to, from) {
296+
for (var propertyName in from) {
297+
if (from.hasOwnProperty(propertyName)) {
298+
to[propertyName] = from[propertyName];
299+
}
300+
}
301+
return to;
302+
};
303+
304+
ao.serialize = function (obj) {
305+
var str = [];
306+
for (var propertyName in obj) {
307+
if (obj.hasOwnProperty(propertyName)) {
308+
str.push(encodeURIComponent(propertyName) + '=' + encodeURIComponent(obj[propertyName]));
309+
}
310+
}
311+
return str.join('&');
312+
};
313+
314+
// Ajax
315+
function getXhr() {
316+
return window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
317+
}
318+
319+
var AjaxOpts = function () { };
320+
AjaxOpts.prototype = {
321+
type: 'GET',
322+
async: true
323+
};
324+
AjaxOpts.prototype.onFail = doNothing;
325+
AjaxOpts.prototype.onSuccess = doNothing;
326+
327+
var done = 4;
328+
329+
ao.ajax = function (userOpts) {
330+
var opts = ao.merge(new AjaxOpts(), userOpts);
331+
var xhr = getXhr();
332+
xhr.open(opts.type, opts.url, opts.async);
333+
xhr.onreadystatechange = function () {
334+
if (xhr.readyState === done) {
335+
var response = Boolean(xhr.responseText) ? JSON.parse(xhr.responseText) : {};
336+
response.statusCode = xhr.status;
337+
if (opts.hasOwnProperty('state')) {
338+
response.state = opts.state;
339+
}
340+
if (xhr.status >= 400) {
341+
opts.onFail(response);
342+
} else {
343+
try {
344+
opts.onSuccess(response);
345+
} catch (err) {
346+
response.error = err;
347+
opts.onFail(response);
348+
}
349+
}
350+
}
351+
};
352+
if (opts.hasOwnProperty('data') === false) {
353+
xhr.send();
354+
return;
355+
}
356+
var dataType, data;
357+
if (opts.hasOwnProperty('isJson') && opts.isJson === true) {
358+
dataType = 'json';
359+
data = JSON.stringify(opts.data);
360+
} else {
361+
dataType = 'x-www-form-urlencoded';
362+
data = ao.serialize(opts.data);
363+
}
364+
xhr.setRequestHeader('Content-Type', 'application/' + dataType);
365+
xhr.send(data);
366+
};
367+
297368
ao.form = function (form) {
298369
if (typeof form === 'undefined') {
299370
form = document.forms[0];

assets/js/contact.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@
2222
message: this.message.value
2323
};
2424

25-
//$.ajax({
26-
// type: 'post',
27-
// url: form.action,
28-
// data: formData,
29-
// xhrFields: {
30-
// withCredentials: false
31-
// }
32-
//}).fail(function () {
33-
// ao.form().error();
34-
//}).done(function () {
35-
// ao.form().ok();
36-
//});
25+
ao.ajax({
26+
type: 'post',
27+
url: form.action,
28+
data: formData,
29+
state: aoForm,
30+
onFail: function(response) {
31+
response.state.error();
32+
},
33+
onSuccess: function(response) {
34+
response.state.ok();
35+
}
36+
});
3737

3838
return false;
3939
};

assets/js/contact.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contact.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ <h1 class="post-title">Contact</h1>
3838
</div>
3939

4040
<div id="complete-ok" class="hidden">
41-
{% include icons/check.html %} <span>Sent</span> <a class="button" href="JavaScript:AgileObjects.Web.contactForm.handleSent(true);">OK</a>
41+
{% include icons/check.html %} <span>Sent</span> <a class="button" href="JavaScript:Ao.Web.contactForm.handleSent(true);">OK</a>
4242
</div>
4343

4444
<div id="complete-error" class="hidden">
45-
{% include icons/error.html %} <span>Error!</span> <a class="button" href="JavaScript:AgileObjects.Web.contactForm.handleSent(false);">OK</a>
45+
{% include icons/error.html %} <span>Error!</span> <a class="button" href="JavaScript:Ao.Web.contactForm.handleSent(false);">OK</a>
4646
</div>
4747
</form>
4848
</div><!-- .inner-->

0 commit comments

Comments
 (0)