Chris Richards
Chris Richards

Reputation: 705

Xpages - Document refreshing with old values

I have some code, that if a user selects a "NO" answer on a certain field (Q1), it removes the answers of sub question related to Q1, and also hides them in the UI.

This works fine, and I do a partial refresh on the div containing all the questions. However, when I then click on the same question selecting answer "Yes" which then shows all the sub questions, so the user can answer them again, they still have the old values attributed.

When I remove the fields, the backend document reflects this correctly, but then when I click the question which then shows the fields again in the UI, they appear in the backend document again, with their original value. If I do a context.reloadPage() at the end of my code, everything works as expected, but this breaks other functionality and I only want a partial refresh. Any ideas? Thanks

try{

    if (compositeData.onClickCode) {
        compositeData.onClickCode.invoke( facesContext, null );
    } 

    document1.save();   
    var fieldName:string = compositeData.fieldName; 

//Blank all IFM fields if user selects no IFM *CHANGE FIELDS ARRAY TO GET FROM KEYWORD*     
if (fieldName == "Q1") {
 print("Yes Q1");
    if(document1.getItemValueString(fieldName)=="No") {
     print("NO IFMS");
        var questionsIFM = ["Q1a", "Q1b", "Q1c", "Q3IFM", "Q3bIFM", "Q3cIFM", "Q3dIFM", "Q4", "Q4a" ,"Q5IFM", "Q6IFM", "Q6aIFM", "Q7IFM",
        "Q7aIFM", "Q7bIFM", "Q8", "Q8a", "Q9IFM", "Q9aIFM", "Q9bIFM", "Q10IFM", "Q11IFM"];  

        var len = questionsIFM.length;
        for (i = 0; i < len; i++) { 
            print("Field Name: " + questionsIFM[i]);
            document1.removeItem(questionsIFM[i]);
        }
            document1.save();
            //context.reloadPage();
    }
}

    var guidanceOptions:string = "";    
    if (compositeData.guidanceOptions) {
        if(@IsMember(document1.getItemValueString(fieldName), compositeData.guidanceOptions)){
            guidanceOptions = document1.getItemValueString(fieldName);
        }   
    }   

    viewScope.guidance = compositeData.fieldName+guidanceOptions;

}catch(e){
    openLogBean.addError(e,this.getParent());
}

Upvotes: 1

Views: 407

Answers (2)

D.Bugger
D.Bugger

Reputation: 2359

I might be late to this party, but I found a way to force a reload from the back-end document. All you have to do is change the id for the dataSource a little, by adding or removing a "0" prefix to the documentId. Something like this:

return (viewScope.idPrefix? "0":"") + param.documentId

and when you want to force a reload during a partial refresh, just set

viewScope.idPrefix= !viewScope.idPrefix;

Don't forget to set ignoreRequestParams to true.

Upvotes: 0

Sven Hasselbach
Sven Hasselbach

Reputation: 10485

Instead of changing the backend document you could update the datasource with doc.setValue('fieldName','')

Alternativly, you could change the scope of the datasource to "request", this forces the datasource to reload the backend document for every request.

EDIT:

Here is an example for updating the datasource directly (Button 'Clear') which always works and updating the component (Button 'Update').

If the button 'Reload' is clicked, changes to in the fields are "stored" in the component. The button 'Update' does a partial refresh (which hides the input field), so the value is lost.

<?xml version="1.0" encoding="UTF-8"?>
<xp:view
    xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:this.data>
        <xp:dominoDocument
            var="document1"></xp:dominoDocument>
    </xp:this.data>

    <xp:div
        id="refreshMe">
        <xp:inputText
            id="inputText1"
            value="#{document1.foo}"></xp:inputText>

        <xp:inputText
            id="inputText2"
            value="#{document1.bar}"
            rendered="#{javascript:facesContext.isAjaxPartialRefresh() == false}">
        </xp:inputText>

    </xp:div>

    <xp:button
        value="Update"
        id="buttonUpdate">
        <xp:eventHandler
            event="onclick"
            submit="true"
            refreshMode="partial"
            refreshId="refreshMe">
        </xp:eventHandler>
    </xp:button>

    <xp:button
        value="Clear"
        id="buttonClear">
        <xp:eventHandler
            event="onclick"
            submit="true"
            refreshMode="partial"
            refreshId="refreshMe">
            <xp:this.action><![CDATA[#{javascript:document1.setValue('foo', '');document1.setValue('bar', '')}]]></xp:this.action>
        </xp:eventHandler>
    </xp:button>


    <xp:button
        value="Reload"
        id="buttonReload">
        <xp:eventHandler
            event="onclick"
            submit="true"
            refreshMode="complete">
        </xp:eventHandler>
    </xp:button>

</xp:view>

Upvotes: 3

Related Questions