Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not set both decoder and beforeSend #76

Open
ghost opened this issue Dec 6, 2012 · 4 comments
Open

Can not set both decoder and beforeSend #76

ghost opened this issue Dec 6, 2012 · 4 comments

Comments

@ghost
Copy link

ghost commented Dec 6, 2012

Errors from json calls are not returned, so the documentation suggests using a decoder. However if I set both decoder and beforeSend in the getSettings variable below the decoder won't be invoked on error. If I set only the decoder, it will be invoked on error.

define('repository', ['amplify'], function (amplify) {

    var repo = function (baseUrl, resourceName) {

        this.list = function (callback, args) {
            return amplify.request({
                resourceId: resourceName,
                data: args,
                success: callback.success,
                error: callback.error
            });
        };

        var setUserToken = function (jqXhr, settings) {
            jqXhr.setRequestHeader("X-UserToken", amplify.store("userToken"));
        };

        amplify.request.decoders.appEnvelope =
                    function (data, status, xhr, success, error) {
                        if (status === "success") {
                            success(data);
                        } else {
                            try {
                                var message = JSON.parse(xhr.responseText);
                                error(status, message.message);
                            } catch (e) {
                                error(status, xhr.responseText);
                            }
                        }
                    }

        var getSettings = {
            url: baseUrl,
            dataType: 'json',
            type: 'GET',
            beforeSend: setUserToken, // This must be commented out or the decoder won't be invoked
            decoder: "appEnvelope"
        };

        amplify.request.define(resourceName, "ajax", getSettings);
    };

    return {
        Repository: repo
    };
});
@elijahmanor
Copy link
Contributor

You found an interesting scenario, congratulations ;) But seriously, sorry
for the inconvenience. I converted your code snippet into a jsFiddle
http://jsfiddle.net/yUs2S/ and tracked down the issue.

Thankfully there is an easy workaround until we get something in place that
takes care of the real problem. You can return true; as the last
statement in your setUserToken function and all should work as expected.

_Elijah Manor
*_Senior Architect & Trainer for appendTo http://appendto.com
*Microsoft Regional Director & Microsoft ASP.NET
MVPhttps://mvp.support.microsoft.com/profile=EEFEC54D-1659-4C89-8423-5370865C8342

ASPInsider http://aspinsiders.com/default.aspx
My profiles: [image: Blogger] http://elijahmanor.com[image:
Twitter]http://twitter.com/elijahmanor[image:
Facebook] http://www.facebook.com/elijahmanor[image:
LinkedIn]http://www.linkedin.com/in/elijahmanor[image:
Delicious] http://delicious.com/elijahmanor

On Thu, Dec 6, 2012 at 6:16 AM, jfitzharris notifications@github.comwrote:

Errors from json calls are not returned, so the documentation suggests
using a decoder. However if I set both decoder and beforeSend in the
getSettings variable below the decoder won't be invoked on error. If I set
only the decoder, it will be invoked on error.

define('repository', ['amplify'], function (amplify) {

var repo = function (baseUrl, resourceName) {

    this.list = function (callback, args) {
        return amplify.request({
            resourceId: resourceName,
            data: args,
            success: callback.success,
            error: callback.error
        });
    };

    var setUserToken = function (jqXhr, settings) {
        jqXhr.setRequestHeader("X-UserToken", amplify.store("userToken"));
    };

    amplify.request.decoders.appEnvelope =
                function (data, status, xhr, success, error) {
                    if (status === "success") {
                        success(data);
                    } else {
                        try {
                            var message = JSON.parse(xhr.responseText);
                            error(status, message.message);
                        } catch (e) {
                            error(status, xhr.responseText);
                        }
                    }
                }

    var getSettings = {
        url: baseUrl,
        dataType: 'json',
        type: 'GET',
        beforeSend: setUserToken, // This must be commented out or the decoder won't be invoked
        decoder: "appEnvelope"
    };

    amplify.request.define(resourceName, "ajax", getSettings);
};

return {
    Repository: repo
};});


Reply to this email directly or view it on GitHubhttps://github.com//issues/76.

@ghost
Copy link
Author

ghost commented Dec 7, 2012

Thanks for the workaround, it solves our problem.

@ghost ghost closed this as completed Dec 7, 2012
@ghost ghost reopened this Dec 12, 2012
@ghost
Copy link
Author

ghost commented Dec 12, 2012

I am reopening this issue as I have found another problem. In the setUserToken function, add another call to jqXhr.setRequestHeader to add another header and run. Amplify will return an error status but there is no error message. Looking in Fiddler I don't see the ajax call being made, so I wonder if Amplify has a problem with multiple request headers.

@CFWinsor
Copy link

I am finding that I have this issue using amplify 1.1.2 and adding the return true; at the end of my beforeBind function as you described does not solve the problem. If beforeBind is uncommented it doesn't go through the decoder function, if it is commented out then it does. Is there an update on this problem? My code is basically identical to that used by jfitzharris above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants