can-connect/data/combine-requests/combine-requests
module
Combines multiple incoming requests into one if possible.
dataCombineRequests( baseConnection )
Overwrites getListData to collect the requested sets for some time. Once that time has expired, it tries to take the union of those sets. It makes requests with those unioned sets. Once the unioned set data has returned, the original requests re satisified by getting subsets of the unioned set data.
Use
Create a connection with the combine-requests plugin like:
var todosConnection = connect([
require("can-connect/data/combine-requests/combine-requests"),
require("can-connect/data/url/url")
],{
url: "/todos"
});
By default, the following will only make a single request if made at the same time:
todosConnection.getListData({})
todosConnection.getListData({userId: 5});
todosConnection.getListData({userId: 5, type: "critical"});
This is because can-set knows that
{userId: 5, type: "critical"} and {userId: 5} are subsets of {}.
For more advanced combining, use set algebra. The following supports combining ranges:
var todosConnection = connect([
require("can-connect/data/combine-requests/combine-requests"),
require("can-connect/data/url/url")
],{
url: "/todos",
algebra: new Algebra(set.props.range("start","end"))
});
Now the following will make single request:
todosConnection.getListData({start: 0, end: 49})
todosConnection.getListData({start: 0, end: 5});
todosConnection.getListData({start: 50, end: 99});