Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CDB: buildScript: Ajax error handling

  • Loading branch information...
commit a0f6c6a8cbe03636f16f4039902e99784dadf95d 1 parent 1fddf75
@mgiulio mgiulio authored
Showing with 58 additions and 27 deletions.
  1. +57 −26 download/js/script.js
  2. +1 −1  download/js/script.min.js
View
83 download/js/script.js
@@ -123,51 +123,82 @@ function build() {
.filter(':checked')
.map(function() { return $(this).attr('id')})
.get(),
- onBuildCompleted
+ onBuildCompleted,
+ onBuildFailure
);
}
function onBuildCompleted(minifiedScript) {
$.n('Build completed');
-
builtScript = minifiedScript;
-
buttons.download.enable();
}
+function onBuildFailure() {
+ $.n.error('Build failed, please retry');
+ builtScript = '';
+ checkboxes.enable();
+ buttons.build.enable();
+}
+
function onDownloadComplete() {
checkboxes.enable();
buttons.download.disable();
buttons.build.enable();
}
-function buildScript(services, success) {
- var out = [];
+function buildScript(services, ok, ko) {
+ var
+ concatenatedSrc = [],
+ jqXHR = []
+ ;
+
$.n('Fetching src modules...');
$.ajax({
url: '../src/core.js',
dataType: 'text',
cache: false
- }).done(function(src) {
- out.push(src);
- // The services scripts are not (necessarily)
- // concatened in the same order as in the services array.
- // We don't need to preserve that order so we can
- // just fire all the script requests (potentially)
- // speeding up the process.
- $.whenArray(
- $.map(services, function(s) {
- return $.ajax({
- url: '../src/services/' + s + '.js',
- dataType: 'text',
- cache: false
- }).done(function(src) {
- out.push(src);
- });
- })).then(function() {
- $.n('All src moduled received');
- $.n('Uglification...');
- success(uglify(out.join(';')));
- });
+ })
+ .fail(function() {
+ $.n.error('Could not retrieve core module');
+ ko();
+ })
+ .done(function(src) {
+ concatenatedSrc.push(src);
+ // The services scripts are not (necessarily)
+ // concatened in the same order as in the services array.
+ // We don't need to preserve that order so we can
+ // just fire all the script requests (potentially)
+ // speeding up the process.
+ $.whenArray(jqXHR =
+ $.map(services, function(s) {
+ return $.ajax({
+ url: '../src/services/' + s + '.js',
+ dataType: 'text',
+ cache: false
+ })
+ .fail(function(jqXHR, err, ex) {
+ if (err == 'abort')
+ $.n.error('Aborted ' + s);
+ else
+ $.n.error('Could not retrieve module ' + s);// + ': ' + err + ', ' + ex);
+ });
+ }))
+ .done(function() {
+ $.n('All src moduled received');
+ $.n('Uglification...');
+ for (var i = 0; i < arguments.length; i++)
+ concatenatedSrc.push(arguments[i][0]);
+ ok(uglify(concatenatedSrc.join(';')));
+ })
+ .fail(function() {
+ var i, x;
+ for (i = 0; i < jqXHR.length; ++i) {
+ x = jqXHR[i];
+ if (!x.isResolved())
+ x.abort();
+ }
+ ko();
+ });
});
}
View
2  download/js/script.min.js
@@ -1,4 +1,4 @@
/**
* Helper function for passing arrays of promises to $.when
* https://gist.github.com/830561
- */function buildScript(a,b){var c=[];$.n("Fetching src modules..."),$.getScript("../src/core.js").done(function(d){c.push(d),$.whenArray($.map(a,function(a){return $.getScript("../src/services/"+a+".js",function(a){c.push(a)})})).then(function(){$.n("All src moduled received"),$.n("Uglification..."),b(uglify(c.join(";")))})})}function onDownloadComplete(){checkboxes.enable(),buttons.download.disable(),buttons.build.enable()}function onBuildCompleted(a){$.n("Build completed"),builtScript=a,buttons.download.enable()}function build(){$.n("Build started"),checkboxes.disable(),buttons.build.disable(),buildScript($('input[type="checkbox"]').filter(":checked").map(function(){return $(this).attr("id")}).get(),onBuildCompleted)}function buildUI(a){$.n(a.length+" services available"),function(b,c,d,e,f){for(c=0,d=$("legend"),e=$('<div class="col"></div>').insertAfter(d);c<b;++c,d=e,e=$('<div class="col"></div>').insertAfter(d))for(f=c;f<a.length;f+=b)e.append('<div><label for="'+a[f]+'">'+'<input type="checkbox" id="'+a[f]+'">'+a[f]+"</label></div>")}(4),checkboxes=$('input[type="checkbox"]').extend({enable:function(){this.each(function(){$(this).removeAttr("disabled")});return this},disable:function(){this.each(function(){$(this).attr("disabled","disabled")});return this}}),buttons.build.disable().click(function(a){a.preventDefault(),build()}),$("form").delegate('form input[type="checkbox"]',"change",function(){var a=0;return function(){this.checked?a++:a--,a>0?buttons.build.enable():buttons.build.disable()}}()),Downloadify.create("button-bar",{filename:function(){return"jquery.lifestream.min.js"},data:function(){return builtScript},onComplete:onDownloadComplete,onError:function(){alert("You must put something in the File Contents or there will be nothing to save!")},transparent:!1,swf:"js/downloadify.swf",downloadImage:"img/download.png",width:100,height:30,append:!0}),setTimeout(function(){buttons.download=$("#button-bar > object").extend({enable:function(){this.css({visibility:"visible"});return this},disable:function(){this.css({visibility:"hidden"});return this}}),buttons.download.disable()},0)}jQuery.whenArray=function(a){return jQuery.when.apply(this,a)},$.n.defaults.timeout=8e3;var buttons={build:$("#button").extend({enable:function(){this.removeAttr("disabled");return this},disable:function(){this.attr("disabled","disabled");return this}}),download:undefined},checkboxes,builtScript="";$.n("Fetching available services..."),$.ajax({url:"services.json",dataType:"json",success:buildUI})
+ */function buildScript(a,b,c){var d=[],e=[];$.n("Fetching src modules..."),$.ajax({url:"../src/core.js",dataType:"text",cache:!1}).fail(function(){$.n.error("Could not retrieve core module"),c()}).done(function(f){d.push(f),$.whenArray(e=$.map(a,function(a){return $.ajax({url:"../src/services/"+a+".js",dataType:"text",cache:!1}).fail(function(b,c,d){c=="abort"?$.n.error("Aborted "+a):$.n.error("Could not retrieve module "+a)})})).done(function(){$.n("All src moduled received"),$.n("Uglification...");for(var a=0;a<arguments.length;a++)d.push(arguments[a][0]);b(uglify(d.join(";")))}).fail(function(){var a,b;for(a=0;a<e.length;++a)b=e[a],b.isResolved()||b.abort();c()})})}function onDownloadComplete(){checkboxes.enable(),buttons.download.disable(),buttons.build.enable()}function onBuildFailure(){$.n.error("Build failed, please retry"),builtScript="",checkboxes.enable(),buttons.build.enable()}function onBuildCompleted(a){$.n("Build completed"),builtScript=a,buttons.download.enable()}function build(){$.n("Build started"),checkboxes.disable(),buttons.build.disable(),buildScript($('input[type="checkbox"]').filter(":checked").map(function(){return $(this).attr("id")}).get(),onBuildCompleted,onBuildFailure)}function buildUI(a){$.n(a.length+" services available"),function(b,c,d,e,f){for(c=0,d=$("legend"),e=$('<div class="col"></div>').insertAfter(d);c<b;++c,d=e,e=$('<div class="col"></div>').insertAfter(d))for(f=c;f<a.length;f+=b)e.append('<div><label for="'+a[f]+'">'+'<input type="checkbox" id="'+a[f]+'">'+a[f]+"</label></div>")}(4),checkboxes=$('input[type="checkbox"]').extend({enable:function(){this.each(function(){$(this).removeAttr("disabled")});return this},disable:function(){this.each(function(){$(this).attr("disabled","disabled")});return this}}),buttons.build.disable().click(function(a){a.preventDefault(),build()}),$("form").delegate('form input[type="checkbox"]',"change",function(){var a=0;return function(){this.checked?a++:a--,a>0?buttons.build.enable():buttons.build.disable()}}()),Downloadify.create("button-bar",{filename:function(){return"jquery.lifestream.min.js"},data:function(){return builtScript},onComplete:onDownloadComplete,onError:function(){alert("You must put something in the File Contents or there will be nothing to save!")},transparent:!1,swf:"js/downloadify.swf",downloadImage:"img/download.png",width:100,height:30,append:!0}),setTimeout(function(){buttons.download=$("#button-bar > object").extend({enable:function(){this.css({visibility:"visible"});return this},disable:function(){this.css({visibility:"hidden"});return this}}),buttons.download.disable()},0)}jQuery.whenArray=function(a){return jQuery.when.apply(this,a)},$.n.defaults.timeout=8e3;var buttons={build:$("#button").extend({enable:function(){this.removeAttr("disabled");return this},disable:function(){this.attr("disabled","disabled");return this}}),download:undefined},checkboxes,builtScript="";$.n("Fetching available services..."),$.ajax({url:"services.json",dataType:"json",success:buildUI})
Please sign in to comment.
Something went wrong with that request. Please try again.