0

I'm successfully using a restJsonData source bound to a dataTable to display a list of projects

<xe:restJsonData var='rest' endpoint="myendpoint" serviceUrl="/projects/" paramCount="limit" paramFirst="offset" paramFirstType="page1" splitPath="items"> </xe:restJsonData>

To enable the users to filter I'd like to also have a combobox driven from the same response data. This initially sounded easy but building a combobox via a repeat control like this doesn't work..(comes out empty)

<xp:comboBox id="comboBox1"> <xp:repeat id="repeat1" rows="30" value="#{rest}" var="project" disableOutputTag="true"> <xp:selectItem itemLabel="#{project.fields[0].values[0].value}" itemValue="#{project.fields[0].values[0].value}"></xp:selectItem> </xp:repeat> </xp:comboBox>

So I believe I need iterate over the json manually and build a collection of selectitems. Fairly straightforward to do but first I need the raw json. I can get it like this..

@Endpoint("myendpoint").xhrGet("/projects/").getData();

but then I assume i'm calling the webservice twice.

so what I'd like to do is to get the raw json from the data source to avoid a 2nd call so I can iterate over it manually. Is that possible? myDs.getData() or something like that.

NB: I know longer term it would be better to do this with some beans however I want to see if it can be done quickly with the out of the box controls.

Thanks!

Martin Holland
  • 291
  • 1
  • 14
  • 1
    You can see the raw JSON using the URL of the XPage, and adding your var at the end, yourPage.xsp\rest Is that what you are asking? – Steve Zavocki Jul 24 '14 at 13:48
  • thanks. I'm trying to get a handle on the raw json as a string in Javascript so I can manually loop through it. e.g. if i have.. I want to be able to do something like var jsonStr = myDS.getData(); – Martin Holland Jul 25 '14 at 07:52
  • I have only used the way I mentioned to verify that the JSON is loading properly and to check the contents. Try Serdar's method below. – Steve Zavocki Jul 25 '14 at 12:32
  • 1
    cheers. the url trick is definitely useful though. I didn't know about it – Martin Holland Jul 25 '14 at 12:52

1 Answers1

0

restJsonData caches the received data within a scope you defined. Therefore it's not a problem how many times you use the data source within the scope you have specified (request/view/session/application).

In detail, restJsonData has a JsonAccessor which actually reads the data. That class (inherited from DataBlockAccessor) prefetches data and use it many times as long as we don't change parameters (e.g. page index, etc).

Data is kept within a data container stored like a bean in scope variables. But I don't really know how to extract data from the data container.

So it's a better idea to use additional repeat to construct your combobox. Your code seems OK, if you post json data structure, we can see if why it's not working.

Additional note: Endpoint.xhrGet() is not cached.

Serdar Basegmez
  • 3,355
  • 16
  • 20
  • Cheers. When you say "restJsonData does not hold data" does that mean if you have one restJsonData datasource and bind two things to it (either 2 views or 1 view and 1 repeat etc) does that mean that the service is triggered twice? If so do you know if there's any kind of caching involved? If the service is called 'per use' and without any caching then I think i'll go straight to the bean approach :) – Martin Holland Jul 25 '14 at 12:41
  • OK, that's a very interesting question and I'd say twice. However, just tested that and in fact, it triggers once! :) FacesContext caches the data as far as I see. I'll check it again... – Serdar Basegmez Jul 25 '14 at 13:40
  • Exactly, I have spoken early. DataBlockAccessor does the magic and it caches the data between scopes (you might define a scope for the data source). Therefore, it doesn't make a difference how many times you have used the data beyond that scope. I will update my answer! – Serdar Basegmez Jul 25 '14 at 13:51
  • Great. that means I can use.. @Endpoint("myendpoint").xhrGet("/projects/").getData(); To get the data as a json string. As it's cached it wont call it again like I originally feared. Learn something every day.. :P – Martin Holland Jul 25 '14 at 14:14
  • No, unfortunately xhrGet is not cached. Only data source is cached. :( – Serdar Basegmez Jul 25 '14 at 14:17