Skip to content

Instantly share code, notes, and snippets.

@stlsmiths
Created August 15, 2012 19:31
Show Gist options
  • Save stlsmiths/3362874 to your computer and use it in GitHub Desktop.
Save stlsmiths/3362874 to your computer and use it in GitHub Desktop.
DT Paginator - remote pagination concepts
/*
We need to somehow build in mapping between the Paginator.Model ATTRS and
remote server request for both "outgoing" and "response" data.
I was thinking of a "server hash" in some manner, that could handle the
translation for known Paginator.Model attributes to both url/querystring
placeholders for outgoing request, and to handle returned data in the
incoming response back from the server (... that need to be loaded back
into the Paginator.Model [think totalRecords])
serverMapping:{
totalItems: 'totalRecords',
itemsPerPage: { toServer:'numPageRecords', fromServer:'pageRecords' },
itemIndexStart: 'startIndex'
// etc...
}
If the "mapping" makes sense, where should it reside ??
at the ModelList (ModelSync.REST) as an ATTR
OR
at the DataTable.Paginator (... the ultimate recipient)
*/
var myModelList = Y.Base.create('myml',Y.ModelList,[Y.ModelSync.REST],{
// not sure of the "perfect" RESTful format here ...
url1: '/my/awesome/REST/server/data/collection/{currentPage},{startIndex}:{numPageRecords}',
url2: '/my/awesome/REST/server/data/collection?currentPage={currentPage}'
+ '&startIndex={startIndex}&numPageRecords={numPageRecords}',
parse: function(resp) {
/* For a sample response JSON like ...
{
totalRecords:2345, pageRecords:200, startIndex:800,
results:[ {...},{...},{...} ]
}
*/
var parsed = Y.JSON.parse(resp),
meta = {},
data = parsed.results;
meta = {
totalItems : parsed[ serverMapping.totalItems ], // equiv to parsed['totalRecords'],
itemsPerPage: parsed[ serverMapping.itemsPerPage.fromServer ], // equiv to parsed['pageRecords'],
// would need to ^^ check for "obj" or something ...
// ...
}
// fire a "response" event, that DT-paginator listens for ...
this.fire( 'response', { meta:meta, results:data });
return false; // so ModelList doesn't send "data" to DataTable !!!
}
},{
ATTRS:{
serverMapping:{}
/* object hash as follows ...
serverMapping:{
totalItems: 'totalRecords',
itemsPerPage: { toServer:'numPageRecords', fromServer:'pageRecords' },
itemIndexStart: 'startIndex'
}
*/
}
})
// OR at the DataTable.Paginator
var dt = new Y.DataTable({
columns: [],
data: myModelList,
paginator: new Y.PaginatorView({...}),
// "maps" Paginator.Model attributes to server querystring AND returned response ...
serverPaginationMap: {
totalItems: 'totalRecords',
itemsPerPage: { toServer:'numRecs', fromServer:'pageRecords' },
itemIndexStart: 'startIndex'
}
}).render('#somewhere');
/*
DataTable.Paginator would handle firing ALL REQUESTS to server, either ModelList OR DataSource,
by constructing the request object ... using the "serverPaginationMap" attribute to construct
the outgoing request,
AND to receive the response ... via ModelList.after('response') or DataSource.after('response')
after the response is received, it would update Paginator.Model "totalItems" via the serverPaginationMap "fromServer"
settings.
and it would continue to fire off pageChange requests and handle the responses.
*/
// initial load ...
dt.data.load({
// build request using serverMapping OR serverPaginationMap
// as a "translator" between Paginator.Model ATTRS and QueryString ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment