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

conditional data cached in ajax request #271

Closed
franszo opened this Issue Dec 21, 2016 · 8 comments

Comments

Projects
None yet
2 participants
@franszo

franszo commented Dec 21, 2016

Hi Im having a problem sending conditional data on each request

for example Im setting data :

var data = {
    addr: '{{query}}',
    lang: lang,
    focus: undefined
};	

and setting a conditional

if ( a == b) {
	data.focus = 'something';
}

focus is not being sent, which is cool, but when condition met it is, after condition doesnt met again, but data.focus is sent with 'something' again, even if printing out the object, doesnt contain 'focus'.

Turned cache = false, and set ttl = 0, neither helped,
any idea whats going wrong?

Thanks

@running-coder

This comment has been minimized.

Owner

running-coder commented Dec 21, 2016

Have you placed this inside a returned function inside the ajax config? (example taken from the configuration source documentation)

// Set a function that return a request object to have "dynamic" conditions
dynamic: true,
source: {
    tag: {
        ajax: function (query) {
            if (query === "hey") {
                query = "hi"
            }
            return {
                url: "http://www.gamer-hub.com/tag/list.json",
                dataType: "jsonp",
                path: data,
                data: {
                    q: query
                }
            }
        }
    }
}
@franszo

This comment has been minimized.

franszo commented Dec 21, 2016

it is like

$.typeahead({
            debug: true,
            compression: true,
            hint: true,
            emptyTemplate: "no result for {{query}}",
            dynamic: true,
            delay: 500,
            source: {
                somthing: {
                    filter: false,
                    display: ['words'],
                    template: function(query, item) {
                        //template here
                    },
                    ajax: function(query) {
                        var data = {
                            addr: '{{query}}',
                            lang: lang,
                            format: 'json',
                        };

                        if (a == b) {
                            data.focus = focus;
                        } else {
                            data.focus = undefined;
                            delete data.focus;
                        }

                        return {
                            type: 'GET',
                            url: url,
                            data: data,
                            path: 'suggestions',
                            beforeSend: function(jqXHR, options) {
                            },
                            callback: {
                                done: function(data, textStatus, jqXHR) {
                                },
                                fail: function(jqXHR, textStatus, errorThrown) {},
                                always: function(data, textStatus, jqXHR) {},
                                then: function(jqXHR, textStatus) {}
                            }
                        };
                    } else {
                        return false;
                    }
                }
            }
        }

strange as logged out, and ajax request working all the time, for some reason data.focus is always being sent after once set by the condition, it doesnt matter if condition doesnt met after it, and reset / undefine / delete data.focus, its still sending it, but console.log(data) doesnt contain it anymore

@running-coder running-coder added this to the 2.7.6 milestone Dec 21, 2016

running-coder added a commit that referenced this issue Dec 21, 2016

@running-coder

This comment has been minimized.

Owner

running-coder commented Dec 21, 2016

found the issue, the data object was cached as you suspected. Please let me know if it fixes the issue. (fix is on the develop branch)

@franszo

This comment has been minimized.

franszo commented Dec 22, 2016

First of all, thanks for taking care and making this better! :)

Second of all, I tried to use the updated code, but had an error message

Uncaught TypeError: Cannot assign to read only property 'addr' of object '#<Object>'
    at Function.jQuery.extend.jQuery.fn.extend (app.js:3514)
    at Function.jQuery.extend.jQuery.fn.extend (app.js:3510)
    at Typeahead.extendXhrObject (app.js:1128)
    at app.js:1168
    at Typeahead.handleRequests (app.js:1298)
    at Typeahead.generateSource (app.js:1055)
    at app.js:914

any idea? when I replaced back latest master branch , it was working again so definitely something with the added new code. Please check

running-coder added a commit that referenced this issue Dec 22, 2016

@running-coder

This comment has been minimized.

Owner

running-coder commented Dec 22, 2016

np, I'm glad to be improving Typeahead and getting some feedback. I've amended last commit, let's take a different approach.

@franszo

This comment has been minimized.

franszo commented Dec 23, 2016

Oh yea, seems this release has fixed the issue :) Ajax is cal working perfectly, and data is changed every time ;) Many thanks again, for fixing this. When this will be merged to master? and will be available on NPM?

running-coder added a commit that referenced this issue Dec 23, 2016

Version 2.7.6
Fix #271 Data is cached inside the xhrObject
@running-coder

This comment has been minimized.

Owner

running-coder commented Dec 23, 2016

There you go, here is your xmas present!

@franszo

This comment has been minimized.

franszo commented Dec 23, 2016

Haha! Merry Xmas!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment