1

I'm having difficulty getting a dynamicContent control to work the way I would like it. I found this bit of code in pasteBin and I think it might just be what I need, but I would like to understand what it is doing before I try implementing it.

<xp:view xmlns:xp="http://www.ibm.com/xsp/core"
                xmlns:xe="http://www.ibm.com/xsp/coreex">
         <xp:button value="Switch!" id="switchButton">
                <xp:eventHandler event="onclick" submit="true"
                        refreshMode="partial" refreshId="dynamicCustomControl">
                        <xp:this.action><![CDATA[#{javascript:viewScope.controlName = 'cc_test2.xsp';
       getComponent('dynamicCustomControl').show(null)}]]></xp:this.action>
                </xp:eventHandler></xp:button>
         <xe:dynamicContent id="dynamicCustomControl">
                        <xp:include id="customControlInluder">
                                <xp:this.pageName>
     <![CDATA[${javascript:viewScope.controlName||"cc_test1.xsp"}]]>
     </xp:this.pageName>
                        </xp:include>
         </xe:dynamicContent>
</xp:view>

In particular I don't understand the syntax of this line:

<![CDATA[${javascript:viewScope.controlName||"cc_test1.xsp"}]]>

I prefer to use viewScope.get("controlName") rather than the short form viewScope.controlName but I don't understand the significance of the || in this line of code.

also the line

<xp:include id="customControlInluder>

is probably an inconsequential typo.

The process looks fairly simple and would appears that it would do the job for me. Just want to make sure I understand it before going down that road.

Edit and update ---

This very brief code snippet might just be one of the best kept secrets around. I have just worked through it and each of the Custom Controls displayed withing the dynamicCustomControl contains a dynamicContent control. So was able to very nicely embedded a dynamicContent inside a dynamicContent. Which to this point I was never really able to get to work correctly. Now it works very smoothly with minimal fuss & muss. Thanks for the comments and assistance.

Bill F
  • 2,057
  • 3
  • 18
  • 39
  • I too prefer in principal to always use the "get" and "put". However Tim Tripcony always told me that scoped Variables area a valid exception for this. I believe since viewScope.varName is usable in EL then. I still use get and put more often then not but I'm open to using the shorter version these days.. – David Leedy Jul 23 '14 at 18:55
  • I believe it makes it more obvious what the code is doing and since my memory is really good but just not very long, I need all the help I can. – Bill F Jul 24 '14 at 02:23
  • Here is an interesting bit of code: (sessionScope.get("ssApplication") = "Demo") ? true : false Which gives a run time error because the assignment wont work. Clearly this is a programming error and I really meant == "Demo" However, if you enter: (sessionScope.ssApplication = "Demo") ? true : false it complies and runs and yields an answer of true. Totally correct but absolutely wrong. This is a simplified version , because in my test ssApplication was "Demo" so the obvious was not that obvious. So I now put the get and put on all my scope variable assignements. – Bill F Jul 24 '14 at 15:15

2 Answers2

4

The JavaScript code viewScope.controlName||"cc_test1.xsp" returns as result the value of viewScope.controlName if it is not

  • 0
  • null
  • undefined
  • NaN
  • "" or '' (=empty string)
  • false

Otherwise it returns "cc_test1.xsp". You can find a detailed explanation here.

This is a short and tricky way of

viewScope.controlName ? viewScope.controlName : "cc_test1.xsp"

or

if (viewScope.controlName) {viewScope.controlName} else {"cc_test1.xsp"}

The id in <xp:include id="customControlInluder"> is not used in code so it doesn't matter how it is called although "customControlIncluder" would sound much better.

I always use the short version for scope variables for getting and setting values and haven't had issues with that yet.

Community
  • 1
  • 1
Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67
1

Bill, think it is just saying "use the viewscope control name, and if null then use cc_test1.xsp". If the first case is true, then the second isn't evaluated.

JavaScript OR (||) variable assignment explanation

Community
  • 1
  • 1
Steve Zavocki
  • 1,840
  • 4
  • 21
  • 35