call partialRefreshGet from SSJS using view.PostSc

2020-08-01 05:24发布

问题:

I have a SSJS running in the BeforePageKoad event. As the last step of the SSJS I want to do a partial refresh. In my script I have this line of code:

view.postScript("partialRefreshGet('#{id:panelAll}')")

When it executes I get an error that says partialRefreshGet is undeffined. I'm guessing I'm missing something in the format of the call, but as I understand it this should work. Anyone done something like this before. If I leave this code out then do a manual refresh of panelAll from a button everything works correctly.

回答1:

You can't use before view.postScript in the beforePageLoad. Try moving it to afterPageLoad or beforeRenderResponse. I recently ran across the same issue.

http://notesspeak.blogspot.com/2013/08/viewpostscript-only-works-for-certain.html

Here is a summary where you can use view.postScript():

  1. onClientLoad = nothing happens
  2. beforePageLoad = XSP error
  3. afterPageLoad = WORKS!
  4. afterRestoreView = nothing happens
  5. beforeRenderResponse = WORKS!
  6. afterRenderResponse = nothing happens

To know why this is happening, read Tim Tripcony's comment at the bottom of the blog post.



回答2:

Just add a scriptblock to your XPage with the required partial refresh.

EDIT:

<xp:scriptBlock id="scriptBlock2" rendered="#{javascript:requestScope.doRefresh}">
    <xp:this.value><![CDATA[
       XSP.addOnLoad( function(){ XSP.partialRefreshGet('#{id:panelAll}') } );
   ]]>
   </xp:this.value>
</xp:scriptBlock>

In your beforePageLoad you then set the requestScope variable if required.



回答3:

This answer might be a little too late, I hope it would be useful for someone.

So, the simple answer is to use the following SSJS to refresh another component: view.postScript("XSP.partialRefreshGet('#{id:Component ID}')")

However, I've had my share of trouble trying it, and as Tim Tripcony suggested, the code seems to be running too soon, while the browser is still receiving the 1st refresh, and it doesn't work. So I tried attaching it in an OnLoad event like so:

view.postScript("XSP.addOnLoad(function() {XSP.partialRefreshGet('#{id:Component ID}')})")

But that also didn't work. So I did it the old way and it worked by letting the script work in a different thread using setTimeout() like this:

view.postScript("setTimeout(function(){XSP.partialRefreshGet('#{id:Component ID}')}, 1)");

and it's working like a charm!