Friday, March 5, 2010

XPages: Tip regarding private scoped variables inside a custom control

One sure way to have a dynamic "private" variable inside a custom control is to name it using the clientId of from one of its components.

Example:
Create a panel that wraps the content of the custom control. Set an id on the panel to wrapper.

To set the scoped variable:
viewScope.put( getClientId( 'wrapper' ), 'someValue' )

To get the scoped variable:
viewScope.get( getClientId( 'wrapper' ) )

This way, you can use the control multiple times in an XPages without having to worry about one custom control overwriting another controls scoped variables. If you need more "private" scoped variables, simply use the clientId as the base. E.g. 'title-' + getClientId( 'wrapper' ).

If you want to use private scoped variables as data source for fields, you need to bind the variables dynamically.

3 comments:

Karthikeyan Alagirisamy said...

Hi Tommy, please help me resolve the following issue.

The following code works fine

var x="#{javascript:@DbName()}";
alert(x)

Where as the following fails,
var x= "#{sessionScope.isDocMarkedForSave}";
alert(x)

I have checked with a computed label. sessionScope.isDocMarkedForSave does exist.

I am getting a run time error when the page gets rendered .
isDocMarkedForSave: com.sun.faces.context.SessionMap.isDocMarkedForSave()

I am puzzled !!!!

Tommy Valand said...

Not sure what the problem might be.

Have you tried using other ways to fetch the scoped variable?
#{sessionScope['isDocMarkedForSave']}
or
#{javascript:sessionScope.isDocMarkedForSave}

Karthikeyan Alagirisamy said...

Thanks :). I am still puzzled !!!!:-

#{javascript:sessionScope.isDocMarkedForSave} works now. But the same did not work yesterday !!!!:-

May be I have got something wrong yesterday.