Skip to content

Commit

Permalink
Fix triggering of queued 'fail' methods on the request. Example:
Browse files Browse the repository at this point in the history
model.save().fail( function() {
    // Should be triggered when the request fails
}
  • Loading branch information
PaulUithol committed Sep 14, 2011
1 parent a2d1da7 commit a04f78f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
9 changes: 7 additions & 2 deletions backbone_tastypie/static/js/backbone-tastypie.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
if ( method === 'create' ) { if ( method === 'create' ) {
var dfd = new $.Deferred(); var dfd = new $.Deferred();


var success = options.success; // Set up 'success' handling
dfd.done( success ); dfd.done( options.success );
options.success = function( resp, status, xhr ) { options.success = function( resp, status, xhr ) {
// If create is successful but doesn't return a response, fire an extra GET. // If create is successful but doesn't return a response, fire an extra GET.
// Otherwise, resolve the deferred (which triggers the original 'success' callbacks). // Otherwise, resolve the deferred (which triggers the original 'success' callbacks).
Expand All @@ -36,6 +36,11 @@
} }
}; };


// Set up 'error' handling
dfd.fail( options.error );
options.error = dfd.reject;

// Make the request, make it accessibly by assigning it to the 'request' property on the deferred
dfd.request = Backbone.oldSync( method, model, options ); dfd.request = Backbone.oldSync( method, model, options );
return dfd; return dfd;
} }
Expand Down
31 changes: 30 additions & 1 deletion test/tests.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ $(document).ready(function() {
}); });


test( "Success callbacks are triggered, receive proper parameters", function() { test( "Success callbacks are triggered, receive proper parameters", function() {
expect( 4 ); expect( 6 );


var emptyResponse = ''; var emptyResponse = '';
var response = { id: 1, 'resource_uri': '/animal/1/' }; var response = { id: 1, 'resource_uri': '/animal/1/' };
Expand All @@ -108,18 +108,47 @@ $(document).ready(function() {
// Request with a response // Request with a response
var animal = new Animal( { species: 'Turtle' } ); var animal = new Animal( { species: 'Turtle' } );
var dfd = animal.save( null, { success: successCallback } ); var dfd = animal.save( null, { success: successCallback } );
// Add another 'success' callback
dfd.done( function() {
ok( true, "Done triggered" );
});


// Do the server's job // Do the server's job
dfd.request.success( response, 'created', xhr ); dfd.request.success( response, 'created', xhr );


// Request without a response right away, response in second request // Request without a response right away, response in second request
animal = new Animal( { species: 'Lion' } ); animal = new Animal( { species: 'Lion' } );
dfd = animal.save( null, { success: successCallback } ); dfd = animal.save( null, { success: successCallback } );
// Add another 'success' callback
dfd.done( function() {
ok( true, "Done triggered" );
});


// Do the server's job // Do the server's job
var secondRequest = dfd.request.success( emptyResponse, 'created', xhr ); var secondRequest = dfd.request.success( emptyResponse, 'created', xhr );
secondRequest.success( response, 'get', { status: 200 } ); secondRequest.success( response, 'get', { status: 200 } );
}); });

test( "Error callbacks are triggered, receive proper parameters", function() {
expect( 2 );

var xhr = { status: 404 };

var errorCallback = function( model, resp, options ) {
equals( resp.status, 404 );
};

// Request with a response
var animal = new Animal( { species: 'Turtle' } );
var dfd = animal.save( null, { error: errorCallback } );
// Add another 'error' callback
dfd.fail( function() {
ok( true, "Fail triggered" );
});

// Do the server's job
dfd.request.error( xhr, 'error', 'Not found' );
});




module("Model url building", { setup: initObjects } ); module("Model url building", { setup: initObjects } );
Expand Down

0 comments on commit a04f78f

Please sign in to comment.