1

I have an XPage which can be used for editing or just reading based on an user's role. Now I need to implement this feature. The solution is really simple - just remove adding, editing, and other HTML elements. The problem is that I only can execute SSJS code, but not CSJS on a page loading. When I tried to do it like this document. in SSJS it gives me a predictable exception - document is not recongized. How do I execute it on page load anyway?

J.Doe
  • 179
  • 14

2 Answers2

5

Don't try to remove HTML elements from rendered page.

Use the property "rendered" instead to determine which controls should be rendered depending on user rights. ("Rendered" means created and sent to browser.)

You can set this property here

enter image description here

or here

enter image description here

Insert SSJS code there which should return true (= visible) or false (= invisible).

Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67
  • Yep, this solution looks like much smater, thank you! – J.Doe Feb 12 '18 at 09:57
  • Here's the question about 'visibility'. Does it only hide or in other words makes dislpay:none, or doesn't include the element at all? Cause I need it to be absolutely absent on a page – J.Doe Feb 12 '18 at 10:04
  • It's the latter. Not rendered elements aren't at the page at all. – Knut Herrmann Feb 12 '18 at 10:12
4

The order of page loading is (XPages events are in bold):

  • a server-side map of all components on the XPage and associated custom controls are loaded into memory
  • beforePageLoad: developer's server-side code runs against those components
  • the server-side components are iterated and updated based on computations or bindings to backend documents
  • afterPageLoad: developer's server side code runs against those components
  • beforeRenderResponse: developer's server-side code runs against those components (using this event means it's also processed during any partial refreshes)
  • render response iterates the components and works out the HTML to pass to the browser
  • afterRenderResponse: developer's server-side code runs against the components

As you can see, SSJS runs only against the server-side map of components, not against the client-side DOM. If you want to amend that, you can use onClientLoad but I don't think that runs after a partial refresh. So your amendments to the DOM may get lost after a partial refresh (you'll need to check). If you want changes to DOM elements to persist, there are two options:

  1. Use dojo.behavior. jQuery may have a corresponding call that could be used.
  2. Write a renderer in Java to change the HTML outputted for a specific type of component.

It sounds like you're strengths are in the client-side, so the former may be the best option.

Paul Stephen Withers
  • 15,699
  • 1
  • 15
  • 33
  • This is a very neat and succinct summary of the page loading events. It should be in the next edition of any XPages book! – TrailDragon Mar 07 '18 at 11:49