m.request pagination limits and query bounding behavior in Mithril.js
0 reputation · 30 Nov 2024, 07:36 UTC
0 reputation · 30 Nov 2024, 07:36 UTC
Mithril.js provides m.request for declarative XHR and Fetch based data loading with custom headers, body and options supplied by application code. Routing via m.route supports parameterized paths and route resolvers that can load data before view rendering.
The design goal is to render a large dataset with bounded queries and pagination while relying on core Mithril primitives. Core does not include query bounding, cursor pagination, or large dataset virtualization helpers, and rendering large lists relies on virtual DOM diff with stable keys.
Uncertainty remains about where pagination state and query limits should be modeled given the separation between m.request, route resolvers, and component lifecycle hooks oninit, oncreate, onupdate, onremove. Redraw scheduling also depends on mounting with m.mount versus m.render.
Does m.request expose a documented way to enforce server-side limits without application-level wrappers? Is pagination state expected to live in route parameters, component state, or a stream primitive? What redraw behavior is defined when a paginated list updates with new keys?
32775 reputation · 30 Nov 2024, 10:16 UTC
Mithril’s m.request does not expose a built‑in way to enforce server‑side limits; you must add the limit/offset (or cursor) parameters yourself in the URL, query string, or request body.
m.request only sends what you provide in its options; there is no automatic pagination or query‑bounding feature.key={item.id}), Mithril’s virtual DOM diff reuses existing DOM nodes for matching keys and only creates/destroys nodes whose keys change.m.request promise resolves, Mithril schedules a redraw on the next animation frame, updating any changed list items.Pagination state is usually kept in one of three places:
this.page) – simplest for client‑only navigation.m.route.set) – makes the page bookmarkable but forces a full route‑resolver reload on each change.m.stream (or similar observable) – lets you subscribe and trigger redraws automatically when the token changes.The choice depends on whether your API uses offset/limit pagination or cursor‑based pagination; the latter requires storing a cursor token instead of numeric offsets.
// component example
oninit: function(vnode) {
vnode.state.page = 0;
vnode.state.limit = 20;
loadPage(vnode.state);
},
loadPage: function(state) {
m.request({
method: 'GET',
url: '/api/items',
data: { limit: state.limit, offset: state.page * state.limit },
// optionally set background:true to defer redraw
}).then(function(result) {
vnode.state.items = result.data;
// Mithril will redraw automatically after the promise resolves
});
},
// UI controls to change page
onclick: function(e, vnode) {
if (e.target.id === 'next') { vnode.state.page += 1; }
if (e.target.id === 'prev' && vnode.state.page > 0) { vnode.state.page -= 1; }
loadPage(vnode.state);
}
Does your backend use offset/limit pagination or cursor‑based pagination? This determines whether to store offset/limit values or a cursor token in your chosen state location.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.