views:

52

answers:

1

I have a variable that I use for caching or temporary storage to avoid repeated requests to the server:

var div2acc = {};

Then there is this function which gets an array from a WCF service or reads it from my div2acc object:

function getAccounts(div_id){
        if(!div2acc.hasOwnProperty(div_id)){
                $.ajax({
                        url: "/Services/OG.svc/GetListOfAccounts",
                        data: { moduleId: mod_id, divisionCode: div_id },
                        success: function(data){
                                        div2acc[div_id] = data;
                        }
                });
        }

        $("#pnlAccounts").setTemplate( $("#tplAccounts").html() );
        //alert(div2acc[div_id]);
        $("#pnlAccounts").processTemplate(div2acc[div_id]);
        $("#pnlAccounts > ol > li").click(function () {
                addDesignation($(this).attr("id"), $(this).text(), "");
        });
}

It checks if div_id is a defined property which would contain an array of accounts copied from the data variable. It then fires up jTemplates and passes the data stored in the div2acc[div_id] property.

The weird thing is that the first time getAccounts is fired with a certain div_id, I get an "fcount is undefined" in Firebug. The second time around, it's works fine and parses the template. I decided to insert an alert() right before the the template is processed and see what div2acc[div_id] is passed to processTemplate(). Turns out, div2acc[div_id] is always undefined the first time getAccounts(div_id) is called, but the second time around, it's fine. What's even more bizarre, is when I insert alert() itself, "fcount is undefined" error doesn't show up anymore and the template is parsed on the first try, right after I click "OK" in the alert window.

I'm clearly misunderstanding something here.

A: 
function getAccounts(div_id) {
    if (!div2acc.hasOwnProperty(div_id)) {
        $.ajax({
            url: "/Services/OG.svc/GetListOfAccounts",
            data: {
                moduleId: mod_id,
                divisionCode: div_id
            },
            success: function (data) {
                div2acc[div_id] = data;
                getAccounts(div_id);
            }
        });
    } else {

        $("#pnlAccounts").setTemplate($("#tplAccounts").html());
        //alert(div2acc[div_id]);
        $("#pnlAccounts").processTemplate(div2acc[div_id]);
        $("#pnlAccounts > ol > li").click(function () {
            addDesignation($(this).attr("id"), $(this).text(), "");
        });
    }
}
Reigel
That eliminates the problem, which I'm assuming has to do with the scope, but I still don't understand. However, this creates another problem which is that the template only gets rendered when the div2acc[div_id] property doesn't exist. However, I want the template to get rendered every time - just request it from the server if it hasn't been yet (and then cache it via property of div2acc). That's why I took those lines out of the if() clause in the first place.
alkos333
ok, try my edit.
Reigel
Great, that worked. Could you please explain why I div2acc does not get its property set right away and getAccounts(div_id) needs to be called upon again. I'm just not sure what's going on here - trying to understand the scoping issues.
alkos333