ShowTable of Contents Where to define a datasource You can define a datasource on different levels:
- in the XPage properties
- in the properties of a custom component
- in the properties of a panel
Use the folowing rule of thumb:
If you need a datasource in your whole XPage and even in custom components which are included on your XPage, then define the datasource in the XPage properties. If you need a datasource only inside a custom component, define it in the properties of that component. If you need to have multiple datasources on one XPage, wrap all logic which works with one datasource inside a panel, and define the datasource in the panel's properties.
The default datasource object If you just have one document datasource on your page, you can use
currentDocument
to access it. That is handy if you want to work with that datasource in customer controls, too. Don't use it if you have multiple datasources on your XPage.
Working with multiple datasources on one page You can have mutiple document datasources on one page, and you can save/submit all of them by different buttons at different types. But to do that, you need some tricks. First, don't set the type of a save button to "submit". A "submit" button ALWAYS submits ALL datasources at once. Regardless if they are defined in the XPage, component or panel properties.
This problem was solved by Jeremy Hodge which wrote an excellent article in the XPages Blog here: http://xpagesblog.com/xpages-blog/2009/8/18/saving-one-out-of-many-data-sources-on-an-xpage-and-how-to-c.html?lastPage=true#comment5154816 .
In short, you need to to this:
1.) Create panels, and define each datasource in one panel's properties.
2.) Put your "save" button inside the panel. Put all fields related to that document source into that panel, too.
3.) Set the button to type "button", set the onclick event to "simple action" with the action "save document".
4.) Add another simple action to the onclick event of the button: Execute script, with a server side javascript to clear all fields after saving the document like this:
datasourcename.replaceItemValue("fieldname", "")
5.) In the panel properties, go to "all properties" -> data -> data -> dominoDocument -> scope and set that to "request". This allows that after saving the document a NEW document is created and represented by the datasource.
Mutiple datasources on one page with different edit mode See Steves Blog for details.
In short: you can make a datasource's editmode independent of the request/URL parameters by setting all properties -> data -> datasource -> ingoreRequestParams = true.
With that, you can have for example one datasource in read mode, and other datasources in edit mode on the same page.
Pass a datasource as parameter to a custom control Create a new custom parameter in your custom control, use the type "Data Sources/DocumentDataSource" (click on the folder button next to the type field).
Go to "All properties" In the custom control's property in your XPage, locate your property, click on the diamond enter a computed value, enter for examle "# mydoc" if your datasource is named "mydoc".
Update 7th march 2011: After adding your custom control to the XPage, go to the source tab, locate the xc: tag of your control and write the property and it's computed value manually like this:
<xc:common_action_save dataSource="#{javascript:document}"
</xc:common_action_save>
In this example my custom control "common_action_save" has the property "dataSource" in which I pass a data source named "document" of my XPage. .
See Designer Wiki for details.
Beware of reserved names Do not name your datasource "header", or it will not work. Found by Patrick Picard .
Computed Datasource Application
If you are required to develop in a development environment before going to production, then the below might be helpful if you need to use another database for your datasource where there is a copy/replica of this in your development environment and production environment.
var curServer = @Subset(@DbName(),1);
var tdTest = []; --- creates new array
tdTest.push(curServer);
tdTest.push("folder\filename.nsf");
var tdProd = []; --- creates new array
tdProd.push(curServer);
tdProd.push("filename.nsf");
if(curServer.toLowerCase().slice(-3) == "dev") { --- slice is similar to using @Left, @Middle, @Right
tdTest[1];
} else {
tdProd[1];
}
The view will have to be manually typed as computing the application doesn't seem to allow a collection of database views to be presented. I am dynamically computing the value so I don't think doing it on page load will make any difference.
contributed by Dwain A Wuerfel, July 7, 2011
|