I'd like to return a list of docs to an array that will be used in some client side JS. In my apsotrophe-pages module I have this apos.docs.find call that is working and returning documents I need to fetch. Now that I have this array of docs, I need to expose the data to the client-side JS.
Reading through the documentation, it looks like pieces are probably recommended for doing this, but it looks like the pieces would need to be put into a widget, and then the widget in the page.
I understand the best practice concern for separation of the modules, but I am just looking to get this data for use with the client as simply and easily as possible (bit of a time crunch).
Is there a simple and easy way for me to expose this autoCompleteData array to the front end, so I can do something similar to below in my client code?
(Thanks for your help! I've had several Apostrophe questions in the last couple days, but I think this one of the final pieces to help me fill in the gaps for connecting backend objects to the front end.)
\lib\modules\apostrophe-pages\views\pages\home.html
<script>
var page = apos.page;
$('#displayDocs').text(page.autoCompleteData);
</script>
\lib\modules\apostrophe-pages\index.js
module.exports = {
types: [
{ name: 'default', label: 'Default Page' },
{ name: 'apostrophe-blog-page', label: 'Blog Index' },
],
autoCompleteData: [],
construct: function(self, options) {
self.pageBeforeSend = function(req, callback) {
let criteria = [
{"type": "apostrophe-blog"},
{"published": true}
];
criteria = { $and: criteria };
self.apos.docs.find(req, criteria).sort({ title: 1 }).toObject(function(err, collection){
self.autoCompleteData = collection;
});
return callback(null);
}
}
}