Skip to content

Instantly share code, notes, and snippets.

@scottgonzalez
Created April 8, 2011 18:15
Show Gist options
  • Save scottgonzalez/910413 to your computer and use it in GitHub Desktop.
Save scottgonzalez/910413 to your computer and use it in GitHub Desktop.
amplify.request.ajaxPollErrors = {
404: true
};
amplify.request.types[ "ajax-poll" ] = function( resource ) {
var baseResourceId = "ajax-poll-" + resource.resourceId;
amplify.request.define( baseResourceId, "ajax", resource );
return function( settings, request ) {
var baseRequest, timeout, ampXHR,
aborted = false;
abort = request.abort,
baseSettings = $.extend( {}, settings, {
resourceId: baseResourceId,
success: function() {
settings.success.apply( this, arguments );
poll();
},
error: function( data, ampXHR ) {
settings.error.apply( this, arguments );
if ( !( ampXHR.status in amplify.request.ajaxPollErrors ) ) {
poll();
}
}
});
function poll() {
if ( aborted ) {
return;
}
timeout = setTimeout(function() {
baseRequest = amplify.request( baseSettings );
}, resource.frequency );
}
baseRequest = amplify.request( baseSettings );
request.abort = function() {
aborted = true;
clearTimeout( timeout );
baseRequest.abort();
abort.call( this );
};
};
};
module( "amplify.request.define - ajax-poll", lifecycle );
asyncTest( "basic", function() {
expect( 33 );
var request,
count = 1;
amplify.request.define( "test", "ajax-poll", {
url: "data/data.json",
dataType: "json",
frequency: 500
});
subscribe( "request.success", function( settings, data, xhr, status ) {
equal( settings.resourceId, "test", "success message: settings.resourceId" );
deepEqual( data, { foo: "bar" }, "success message: data" );
ok( "abort" in xhr, "success message: xhr object provided" );
equal( status, "success", "success message: status" );
});
subscribe( "request.complete", function( settings, data, xhr, status ) {
console.log( "complete" );
equal( settings.resourceId, "test", "complete message: settings.resourceId" );
ok( "abort" in xhr, "complete message: xhr object provided" );
if ( count !== 2 ) {
deepEqual( data, { foo: "bar" }, "complete message: data" );
equal( status, "success", "complete message: status" );
} else {
equal( data, null, "complete message: data" );
equal( status, "abort", "complete message: status" );
start();
}
count++;
if ( count === 2 ) {
request.abort();
}
});
subscribe( "request.error", function( settings, data, xhr, status ) {
if ( count !== 2 ) {
ok( false, "error message published" );
return;
}
equal( settings.resourceId, "test", "error message: settings.resourceId" );
equal( data, null, "error message: data" );
ok( "abort" in xhr, "error message: xhr object provided" );
equal( status, "abort", "error message: status" );
});
request = amplify.request({
resourceId: "test",
success: function( data, xhr, status ) {
deepEqual( data, { foo: "bar" }, "success callback: data" );
ok( "abort" in xhr, "success callback: xhr object provided" );
equal( status, "success", "success callback: status" );
},
error: function( data, xhr, status ) {
if ( count !== 2 ) {
ok( false, "error callback invoked" );
return;
}
deepEqual( data, null, "error callback: data" );
ok( "abort" in xhr, "error callback: xhr object provided" );
equal( status, "abort", "error callback: status" );
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment