This is a migrated thread and some comments may be shown as answers.

"this" in Telerik client events?

1 Answer 52 Views
TabStrip
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 26 Jan 2010, 06:23 PM
I am using a telerik:RadTabStrip in a .ascx user control.  I want to call a function in a javascript object when the client tab is changed.

In the code-behind, I'm doing:

radTabStrip.OnClientTabSelected = "foo.clientTabSelected"

Where "foo" is a javascript object that contains some data, and a number of functions - one of which is clientTabSelected.

function Foo() 
    this.selectedTab = loadCookie('foo.cookie'); 
 
 
Foo.prototype = { 
    clientTabSelected: function(sender, eventArgs) 
    {
        debugger;
        var tab = eventArgs.get_tab(); 
        if (tab) 
            this.selectedTab = tab.get_index(); 
        storeCookie('foo.cookie'this.selectedTab); 
    }, 
    //... 
 

The object "foo", of course, is initialized prior to the user clicking a tab.

The problem is that when the user clicks on the tab, the "clientSelected" function is called, but not on the object "foo". In other words, when I trace through the execution, the correct function is being called, but within the function, "this" is not set to the "foo" object.

Any ideas as to how I can connect a Telerik event to a function in a specific object?



1 Answer, 1 is accepted

Sort by
0
Jeff
Top achievements
Rank 1
answered on 26 Jan 2010, 08:30 PM
OK, nevermind.

This is a generic javascript problem, not specific to the RadTabStrip.

Solution is to create a function within your class constructor, that uses a copy of "this":

function Foo() 

    this.selectedTab = loadCookie('foo.cookie'); 

    var me = this
    this.clientTabSelected = function(sender, eventArgs) 
    {
        // use "me" in place of "this"
        me.clientTabSelectedEx(sender, eventArgs);
    } 
}
 
Foo.prototype = { 
    clientTabSelectedEx: function(sender, eventArgs) 
    {
        debugger;
        var tab = eventArgs.get_tab(); 
        if (tab) 
            this.selectedTab = tab.get_index(); 
        storeCookie('foo.cookie'this.selectedTab); 
    }, 
    //... 
 

Tags
TabStrip
Asked by
Jeff
Top achievements
Rank 1
Answers by
Jeff
Top achievements
Rank 1
Share this question
or