In the Slate page that discusses creating and retrieving object sets (linked above), there is a section that briefly discusses using events named getNextPage and getPreviousPage to page through an object set. Can anyone provide an example of how to do this?
The getNextPage and getPreviousPage are very easy to use - an object set, say s_object_set_1 responds by fetching another or previous page (respectively) of objects whenever the Action s_object_set_1.getNextPage / s_object_set_1.getPreviousPage is triggered, no additional configuration required.
The objects of the currently loaded page can be accessed via Handlebars syntax like {{s_object_set_1.objectRids}} (which is an array) and the object set keeps track of that currently loaded page internally.
Below is an example of an app which displays the first object of the currently loaded page of s_object_set_1 in an Object Card widget. The Actions are configured to trigger in response to clicks on buttons below the card, and the card gets its rid to display as {{s_object_set_1.objectRids.[0]}}
You got me on the right track, thank you! In my case, I want to page through the objects without waiting for a user interaction, e.g., button click. I don’t know if this is great but it works…
In the Interaction tab, I added a state variable named ‘object_set’ (that maps to the object set) and an event named ‘nextPage’
I added a trigger – the ‘nextPage’ event triggers the object set’s getNextPage function
Then I added this to the Javascript:
const objectSetPages = [];
let numObjectsCollected = 0;
SlateFunctions.watch('object_set', () => {
collectObjects();
});
collectObjects();
function collectObjects() {
const objectSet = SlateFunctions.getState().object_set;
objectSetPages.push(objectSet.data);
numObjectsCollected += objectSet.objectRids.length;
if (numObjectsCollected < objectSet.totalObjects) {
SlateFunctions.triggerEvent('nextPage');
}
else {
// We have all the objects; now display them
}
}
That looks fine of course! There is some inherent delay in how events/actions are processed once triggered so there should be no risk with getting rate-limited by the Objects service - if you run into any issues of that sort, adding a setTimeout to your code should help with that.
You can also consider changing the pageSize inside your Object set configuration if you end up producing a lot of calls (I believe up to 10000 should be accepted by the server).
Saying this I would likely add some sort of upper bound (in addition to objectSet.totalObjects) in case the object set was really huge - depending on your use case - but if you find yourself in a need of loading data for, say, millions of objects, then Aggregations inside Object set might be a better tool (again, depends on why you load them in the first place).
As it is, I’m displaying all the objects at once so I need to load them all. And I don’t expect to load on the order of millions. That said, if that changes, I will keep your pointers in mind.
In particular, it seems like the object set paging mechanism would pair nicely with a widget that has paging functionality, e.g., a table/grid widget.
On last point, totally - you could have buttons to move between pages etc. or detect scroll events on a list to load next page when the user scrolls to the bottom.