开发者

javascript global variable returning to previous state on next event call

So i have the global variable

var INSIDE_GLOBAL = {} ;
INSIDE_GLOBAL.current_search = get_new_current_search();

function get_new_current_search() {

    return {
        stack:[],
        search_options: {
            keywords: ""
            },
        };
}

Then, I setup handlers for clicking different div sections in an accordion. This adds a new section to the accordion, makes it the currently viewed section, and sets up the click handlers for the next sections with the same function (setup_search_click_handlers).

function setup_search_click_handlers() {
    $('.search_option').unbind("click");
    $('.search_option').bind("click", function(e) {

        var new_sub_group = $(this).attr('id');

        $("#new_search_panel").bind("accordionchange", function(event, ui) { 
            $("#new_search_panel").unbind("accordionchange");

            //push new section onto the current searches
            INSIDE_GLOBAL.current_search.stack.push(new_sub_group);

            /* pseudo code */
            accordion_add_sect开发者_高级运维ion_and_select_that_section( with_callback: setup_search_click_handlers );
        });

        $("#new_search_panel").accordion("activate",-1);    //Collapse the accordion, calls the newly binded change             

    });

}

At the end of the first click, INSIDE_GLOBAL.current_search.stack has an element in it; However, when the next click event happens and the binded function called, INSIDE_GLOBAL.current_search.stack is back to being empty. Can't figure out why.

I'm assuming it has something todo with the scope of the different call backs, but really not sure.

In firebug, I can see the Window INSIDE_GLOBAL changing correctly, then being "reset" to where the stack array is empty again


Just figured it out. Makes sense that I would spend hours trying to figure out the issue, then find it moments after posting here.

I had just added the stack array to my code and changed over some indexes being passed around in methods to just using the stack.length field.

I have a binding elsewhere that calls a function whenever a the accordion minimizes. This is used when the person clicks a previous section in the accordion (going backwards in the search). It checks a few parameters to make sure this is the case and removes the sections of the accordion after the one the user clicked. while doing this it also calls stack.pop() in order to keep the backend data up to date.

By changing from using index variables to the length variable, the first time the accordion minimized, this check would incorrectly pass and pop the just variable to the stack...

Here's part of the code for whoever was curious

function setup_return_to_previous_handlers() {

    var event_function = function(event, ui) { 

        var active_index = $("#new_search_panel").accordion( "option", "active" );
        var index = INSIDE_GLOBAL.current_search.stack.length; //BUG here: needs to be length-1;
        //alert("accord_change: active:"+active_index+" index:"+index);
        if (    typeof active_index==="number" &&   //Filter's active === false, if user clicked last section
                active_index >= 0 &&                //Filters refreshes
                active_index != index ) {           //User clicked previous section
            $("#new_search_panel").unbind("accordionchange");
            bind_search_buttons();
            //alert("inside");
            for ( ; index > active_index; --index) {
                /* remove accordion sections */

                INSIDE_GLOBAL.current_search.stack.pop(); //Bug: Shouldn't have been called
            }

        }
    };
    $("#new_search_panel").unbind("accordionchange");
    $("#new_search_panel").bind("accordionchange", event_function); 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜