Forum Discussion
Hi charlesssgraham,
You could accomplish this by creating a custom endpoint that makes the API call and returns the LiQL data, and then using jQuery/Ajax to call the endpoint in your component.
On a related note, rather than using the <script> tag, you'll want to leverage the liaAddScript FreeMarker directive instead to wrap your client-side code, as it is much safer and avoids timing issues on dependencies.
Hope this helps.
- charlesssgrahamContributor
jeffshurtliff Thanks for the reply! I'm a bit stuck on how to translate the following query into a custom endpoint:
"SELECT id, login, email, rank.name, rank.simple_criteria.role.id FROM users"
The example given in the docs is a bit simplistic.. Could you give me a pointer or two on how to convert the above LiQL? After the endpoint is constructed, do I call it between the <@liaAddScript> tags or do I pass it into a fetch( )?
Thanks!
- JavidHKhoros Staff
charlesssgraham You can add your query ("SELECT id, login, email, rank.name, rank.simple_criteria.role.id FROM users") to a custom component in studio using the below API V2:
<#assign query = restadmin("2.0","/search?q=" + "SELECT id, login, email, rank.name, rank.simple_criteria.role.id FROM users"?url).data.items/> <#list query as q> ${q.id} ${q.login} ${q.email} ${q.rank.name} <#if q.rank.simple_criteria.role?? && q.rank.simple_criteria.role.id??> ${q.rank.simple_criteria.role.id} </#if>, </#list>
Then you can add this custom component to your desired page in your community in studio.
Hope this helps.
- MohammedFModerator
charlesssgraham If any of the comments were helpful, would you mind marking it as an accepted solution as it may help other users who may have similar requests?
- charlesssgrahamContributor
Done! One more question: is there an equivalent of logging query results to the console?
- JavidHKhoros Staff
Create a custom endpoint (I'm naming it as q) in the studio:
<#assign query= restadmin("2.0","/search?q=" + "SELECT id, login, email, rank.name, rank.simple_criteria.role.id FROM users"?url).data.items/> ${query}
Create a custom component (naming it as component_q) in the studio:
<#assign communityId = community.id /> <#assign communityUrl = http.request.url /> <#assign lengthOfDomain = communityUrl?index_of("/") /> <#assign communityDomain = communityUrl?substring(0,lengthOfDomain) /> ${communityUrl} ${communityId} <@liaAddScript> ;(function($){ $.ajax({ type:"POST", url:'${communityDomain}/plugins/custom/qa/johndowden/q', contentType: 'application/json', success: function(res) { console.log(res); console.log("Results are listed"); }.bind(this), error: function(xhr, status, err) { console.error(xhr, status, err.toString()); console.log("Unable to view the results"); }.bind(this) }); })(LITHIUM.jQuery); </@liaAddScript>
Note: The URL must be your community URL of the endpoint in the studio.
When you add the component_q to any page in your community using studio, you can view the results in the console.
Hope this helps.
If this answers your question, would you please mark it as an accepted solution.
Related Content
- 2 years ago
- 2 years ago
- 4 years ago
- 3 years ago