Skip to content

Commit

Permalink
Make better use of deferred.resolve/resolveWith; test success callbacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulUithol committed Aug 31, 2011
1 parent c26531c commit 41ca1ba
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
12 changes: 6 additions & 6 deletions backbone-tastypie.js
Expand Up @@ -19,20 +19,20 @@
var dfd = new $.Deferred();

var success = options.success;
dfd.done( success );
options.success = function( resp, status, xhr ) {
// If create is successful and doesn't return a response, fire a GET.
// Otherwise, trigger the original 'success'.
// If create is successful but doesn't return a response, fire an extra GET.
// Otherwise, resolve the deferred (which triggers the original 'success' callbacks).
if ( xhr.status === 201 && !resp ) { // 201 CREATED; response null or empty.
var location = xhr.getResponseHeader('Location');
var location = xhr.getResponseHeader( 'Location' );
return $.ajax( {
url: location,
success: [ success, dfd.resolve ],
success: dfd.resolve,
error: dfd.reject
});
}
else {
success( resp, status, xhr );
return dfd.resolve();
return dfd.resolveWith( options.context || options, [ resp, status, xhr ] );
}
};

Expand Down
52 changes: 43 additions & 9 deletions test/tests.js
Expand Up @@ -58,33 +58,67 @@ $(document).ready(function() {


test("Two requests on creation (when the response is empty)", function() {
expect( 1 );
expect( 2 );

var animal = new Animal( { species: 'Turtle' } );
var response = { objects: { id: 1, 'resource_uri': '/animal/1/' } };
var emptyResponse = '';
var response = { id: 1, 'resource_uri': '/animal/1/' };
var xhr = { status: 201, getResponseHeader: function() { return '/animal/1/'; } };

var dfd = animal.save();
dfd.done( function() {
ok( animal.get('id') === 1 );
equals( animal.id, '/animal/1/' );
equals( animal.get( 'id' ), 1 );
});

// Do the server's job
var secondRequest = dfd.request.success( '', 'created', { status: 201, getResponseHeader: function() { return '/animal/1/'; } } );
secondRequest.success[0]( response, 'get', { status: 200 } );
secondRequest.success[1]( response, 'get', { status: 200 } );
var secondRequest = dfd.request.success( emptyResponse, 'created', xhr );
secondRequest.success( response, 'get', { status: 200 } );
});

test("No extra 'GET' on creation when there is a response", function() {
expect( 2 );

var animal = new Animal( { species: 'Turtle' } );
var response = { objects: { id: 1, 'resource_uri': '/animal/1/' } };
var response = { id: 1, 'resource_uri': '/animal/1/' };
var xhr = { status: 201, getResponseHeader: function() { return '/animal/1/'; } };

var dfd = animal.save();
dfd.done( function() {
ok( animal.get('id') === 1 );
equals( animal.id, '/animal/1/' );
equals( animal.get( 'id' ), 1 );
});

// Do the server's job
dfd.request.success( response, 'created', { status: 201, getResponseHeader: function() { return '/animal/1/'; } } );
dfd.request.success( response, 'created', xhr );
});

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

var emptyResponse = '';
var response = { id: 1, 'resource_uri': '/animal/1/' };
var xhr = { status: 201, getResponseHeader: function() { return '/animal/1/'; } };

var successCallback = function( model, resp, xhr ) {
equals( resp.id, 1 );
equals( model.id, '/animal/1/' );
};

// Request with a response
var animal = new Animal( { species: 'Turtle' } );
var dfd = animal.save( null, { success: successCallback } );

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

// Request without a response right away, response in second request
animal = new Animal( { species: 'Lion' } );
dfd = animal.save( null, { success: successCallback } );

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


Expand Down

0 comments on commit 41ca1ba

Please sign in to comment.