An introduction to Sexy.js: a lightweight JavaScript library which provides enhanced Sequential Ajax (Sajax) functionality and a sleek facade to jQuery’s native Ajax methods.
3. Sexy.js
A lightweight JavaScript library which provides enhanced
Sequential Ajax (Sajax) functionality and a sleek facade to
jQuery’s native Ajax methods.
4. Sexy.js
Facilitates combination and manipulation of data from
multiple sources
Speeds loading of scripts, stylesheets, and data using
parallel asynchronous HTTP requests
5. Ugly.js
$.getJSON('articles.json', function (articles) {
$.getJSON('comments.json', function (comments) {
$.getJSON('ratings.json', function (ratings) {
$.each(comments, function (i, comment) {
articles[comment.articleId].comments.push(comment);
});
$.each(ratings, function (i, rating) {
articles[rating.articleId].rating = rating;
});
});
});
});
6. Sexy.js
Sexy
.json('articles.json')
.json('comments.json', function (comments, status, articles) {
$.each(comments, function (i, comment) {
articles[comment.articleId].comments.push(comment);
});
return articles;
})
.json('ratings.json', function (ratings, status, articles) {
$.each(ratings, function (i, rating) {
articles[rating.articleId].rating = rating;
});
});
10. Sequential Ajax (Sajax)
Simultaneous request/sequential response Ajax
Each successive callback receives the return value of
the previous callback as an additional argument
38. Sexy.js API
Core Methods
html (url, callback)
json (url, callback)
jsonp (url, callback)
script/js (url, callback)
style/css (url, callback)
text (url, callback)
xml (url, callback)
39. Sexy.js API
Core Methods
html (url, callback) html (settings)
json (url, callback) json (settings)
jsonp (url, callback) jsonp (settings)
script/js (url, callback) script/js (settings)
style/css (url, callback) style/css (settings)
text (url, callback) text (settings)
xml (url, callback) xml (settings)
40. Sexy.js API
callback (data, status, previous)
Each callback receives three arguments:
data
status
previous - the return value of the previous callback
(considering a fourth) next or this
41. Sexy.js API
Additional Methods
setup (settings)
Similar to jQuery.ajaxSetup, but limited to
subsequent calls of a Sexy instance
Can be unset with setup()
bundle (url1, url2, ..., callback)
Client-side JavaScript packaging
Multiple file, single <script> injection
Sequence is preserved
58. Using Sexy.js
Bundling JavaScripts
Sexy
.bundle(
'/js/bam.js',
‘/js/bam.datagrid.js',
‘/js/bam.stats.js',
‘/js/bam.stats.widget.js',
‘/js/bam.stats.widget.leagueSelector.js',
‘/js/bam.stats.app.js',
‘/js/bam.stats.app.sortablePlayerList.js',
function (src) {
SortablePlayerList.init();
}
);
59. Known Issues
Unable to detect error events for remote requests
jsonp (remoteUrl)
script (remoteUrl)
style (remoteUrl)*
*style (remoteUrl) will actually fire success event even if
load was unsuccessful (ie. 404)
63. Sexy is the new Sexy
Sexy() == new Sexy()
function Sexy (cfg) {
// Allow instantiation without new keyword
if (!(this instanceof Sexy)) {
return new Sexy(cfg);
}
this.cfgs = [];
this.setup(cfg);
this.evt = $(this);
}
64. Take it off... take it all off!
Sexy.method(url) == (new Sexy()).method(url)
// Alias instance methods as static methods of constructor
$.each(Sexy.prototype, function (name, method) {
Sexy[name] = function () {
return method.apply(new Sexy(), arguments);
};
});