Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

amplifyjs decoders and ajax beforeSend

Alright, working on getting amplify decoders. Strange issue. If I have a beforeSend attached to my request, the decoder does not fire. remove the beforeSend and the decoder fires.

Here are the two examples.

  1. With no beforeSend.

http://jsfiddle.net/sujesharukil/Td2P4/12/

  1. With beforeSend

http://jsfiddle.net/sujesharukil/Td2P4/14/

Can someone tell me what is happening? why wouldnt the decoder work if I have a beforeSend? I am assuming the decoder should fire after the request is received, so beforeSend should not have any impact on it!

note: stackoverflow wants me to post code here, not just fiddles

//please check the fiddles

amplify.request({
    resourceId: "testRequest",
    data: {
        json: JSON.stringify({
            text: 'hello world'
        })
    },
    success: function(data, status) {
        console.log(data, status);
        $('.messages').append('<div> text retrieved: ' + data.text + '</div>');
    },
    error: function(status, xhr) {
        console.log(xhr);
    }
});​

Help?

-Suj

like image 201
Sujesh Arukil Avatar asked Jan 15 '23 02:01

Sujesh Arukil


1 Answers

Ok, figured it out.

in amplifyjs this section caught my eye

beforeSend : function( _xhr, _ajaxSettings ) {

                xhr = _xhr;
                ajaxSettings = _ajaxSettings;
                var ret = defnSettings.beforeSend ?
                    defnSettings.beforeSend.call( this, ampXHR, ajaxSettings ) : true;
                return ret && amplify.publish( "request.before.ajax",
                    defnSettings, settings, ajaxSettings, ampXHR );
            }
        });

note that, it will call beforeSend if it is specified, else set var ret is set to true

if set to true, it will then publish "request.before.ajax"

way down in the file, amplify listens for this message

amplify.subscribe( "request.before.ajax", function( resource, settings, ajaxSettings, ampXHR ) {
var _success = ampXHR.success,
    _error = ampXHR.error,
    decoder = $.isFunction( resource.decoder )
        ? resource.decoder
        : resource.decoder in amplify.request.decoders
            ? amplify.request.decoders[ resource.decoder ]
            : amplify.request.decoders._default;

if ( !decoder ) {
    return;
}

function success( data, status ) {
    _success( data, status );
}
function error( data, status ) {
    _error( data, status );
}
ampXHR.success = function( data, status ) {
    decoder( data, status, ampXHR, success, error );
};
ampXHR.error = function( data, status ) {
    decoder( data, status, ampXHR, success, error );
};

});

so, if you have a beforeSend and if it does not return true, the message is never published and the decoders are never hit!

the solution?

return true from your beforeSend function

amplify.request.define("testRequest", "ajax", {

url: "/echo/json/",
dataType: 'json',
type: 'POST',
decoder: function(data, status, xhr, success, error) {
    console.log('decoder fired');
    $('.messages').append('<div>decoder fired </div>');
    success(data);
},
beforeSend: function(xhr){
//not doing anything here, just logging;
    console.log('before send fired');
    $('.messages').append('<div>before send fired </div>');
    return true; //this is the key
}

});

works like a charm! Hope this helps someone else trying to figure this out!

like image 98
Sujesh Arukil Avatar answered Jan 29 '23 21:01

Sujesh Arukil