Showing posts with label browser script. Show all posts
Showing posts with label browser script. Show all posts

Sunday, November 11, 2012

Browser Scripts–‘this’ is a problem

 

One issue I have faced numerous times with Siebel’s browser scripts is that the ‘this’ reference is not recognized when invoked in a separate function.

This code works fine when written directly in the PreInvoke section of the applet:

function Applet_PreInvokeMethod(name, inputPropSet)
{
    if (name == "CustomMethod")
    {
        alert(this.Name());
        return ("CancelOperation");
    }
    return ("ContinueOperation");
}

 

But if you decide to move the code into a separate function of its own:

function Demo()
{
    alert(this.Name());
}

function Applet_PreInvokeMethod(name, inputPropSet)
{
    if (name == "CustomMethod")
    {
        Demo();
        return ("CancelOperation");
    }
    return ("ContinueOperation");
}

..the system will start giving errors saying method not defined.  This really gets in the way when there is not of field access and manipulation required in the function. One way out is to pass the this reference directly as an argument into the function.

function Demo(applet)
{
    alert(applet.Name());
}

function Applet_PreInvokeMethod(name, inputPropSet)
{
    if (name == "CustomMethod")
    {
        Demo(this);
        return ("CancelOperation");
    }
    return ("ContinueOperation");
}

 

Another way I have seen recently is to use a global variable for the applet and use that instead of the this. The variable has to be initialized in Applet_Load event

declarations()
{
    var g_oApplet;
}
function Applet_Load()
{
    g_oApplet = this;
}
function Demo()
{
    alert(g_oApplet.Name());
}
function Applet_PreInvokeMethod(name, inputPropSet)
{
    if (name == "CustomMethod")
    {
        Demo();
        return ("CancelOperation");
    }
    return ("ContinueOperation");
}