The discussion revolves around pulling data from the database for theme components in Discourse. AstonJ initially asked for a guide or example on how to list the latest 20 publicly visible topics in a side column. justin suggested using an AJAX call to the relevant endpoint, such as /latest.json, to retrieve the desired data.
AstonJ then inquired about reusing feeds/data that Discourse already provides, so that the user’s settings are honored. justin confirmed that using the associated endpoints will respect the user’s settings.
AstonJ questioned the efficiency of this approach compared to querying the database directly and asked for guides or tutorials on fetching data from endpoints into theme components. justin provided a link to a guide on adding a featured topic list to the Discourse homepage, which demonstrates how to use an endpoint to retrieve data and can be replicated with other endpoints.
The discussion highlights the benefits and potential drawbacks of using AJAX calls to endpoints versus querying the database directly, and the need for more guides and tutorials on this topic.
Anybody know if there is a guide or example for this anywhere please?
Let’s say for example I use a theme component to create a side column, how can I list the latest 20 publicly visible topics (or if it’s easier, from categories that I list in the query) in that side column/theme component?
There’s a couple of ways to do this. The simplest is to use an ajax call to the endpoint in the rails API that serves the data you want to display. If you want the latest 20 topics, then you might want to try /latest.json.
The challenge is if you need to combine info from multiple endpoints you then wait on multiple calls to complete which can really slow performance down in the theme component.
Do you think that that would be more or less the same as just hitting the DB with whatever queries those endpoints do anyway? (Or would it use significantly more resources since it could be the same as having to load 5 additional pages - one for each endpoint).
Either way I’d love to experiment with this - are there any guides or tutorials that show you how to fetch data from an endpoint into a theme component? (Or would it be better/more efficient to make a plugin instead?)
I know how to create a theme component and a ‘dummy’ plugin, but haven’t found any guides that show you how to fetch data from the DB into them.