PermaLink XPages - How to pass a Document Data Source to a Custom Control08/17/2009 06:29 AM
None
In a previous article I explained how you can use the undocumented currentDocument data source object to get the data source for the XPage from a Custom Control.  This works great if the XPage has one data source. What if you have multiple data sources?  For example, you want to include a custom control within a repeat control that is accessing multiple documents from a document collection or a view.  The currentDocument variable would only point to the first data source on the XPage.

At first glance, looking at the data types that are available for parameters in a Custom Control, you cannot pass a data source object. The type list drop down is a list of standard JavaScript variable types.

Image:John's Blog - XPages - How to pass a Document Data Source to a Custom Control



But, if you click on the folder icon next to the Type field you will see an extended list of type available.

Image:John's Blog - XPages - How to pass a Document Data Source to a Custom Control


Then from your XPage you can pass the datasource using computed JavaScript, such as: "OrderDetails.getDocument();"

*updated - see note below

Image:John's Blog - XPages - How to pass a Document Data Source to a Custom Control

Binding fields on a Custom Control to a dynamic datasource
The second part of this can be tricky.  On your Custom Control you cannot use the "Simple Data Binding" option to bind your fields since you have no datasource set for the Custom Control, the datasource is dynamic.  

The perfect solution for this is to use the Advanced -> Expression Language (EL) binding.  For example, to bind the field to the "Comment" field, we would use "compositeData.docdatasource.Comment".  Where....
- compositeData holds all the parameters for the Custom Control
- docdatasource is the parameter that we created that receives the datasource
- Comment is the field name on the Notes form

Image:John's Blog - XPages - How to pass a Document Data Source to a Custom Control

Now, with that said, there is an issue with this in 8.5.  DDE tries to convert the EL expression to a Simple data binding when you save and reopen the Custom Control. So it works the first time you enter it, but loses the binding when you edit the Custom Control.  However, it looks like this is fixed in 8.5.1.

BUT, no sweat, I have a workaround.  If you select JavaScript for the binding type, select "Compute on page load", and then enter the formula as '#{compositeData.docdatasource.Comment}' it will continue to work.

Image:John's Blog - XPages - How to pass a Document Data Source to a Custom Control


Here's an example of the Custom Control used with multiple Data Sources in action:

Image:John's Blog - XPages - How to pass a Document Data Source to a Custom Control

You can download this example here.

*update - by passing the NotesXSPDocument instead of the NotesDocument, the fields will automatically handle whether the document is in read or edit mode.

To do this, select "ModelDataSource" as the type for the property.  Then when passing the datasource from your XPage change the JavaScript from "OrderDetails.getDocument()" to just "OrderDetails".

-John


Technorati:

This page has been accessed 245 times. .
Comments :v
No comments.
Search
Partner with us
Need help on your XPages projects?

Talk to the experts! Read more...
XPage Examples
By Category
About Me
Downloads
My Links
Monthly Archive
Powered by
Blogsphere
Lotus Domino ND7 RSS News Feed RSS Comments Feed Geo URL netcraft RSS Validator Lotus Geek Chris. A. Brandlehner OpenNTF BlogSphere