Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsmith/80936 to your computer and use it in GitHub Desktop.
Save lsmith/80936 to your computer and use it in GitHub Desktop.
// Declare the DS config during construction. Also leverage the inline override
// support to replace the DS native setInterval implementation with a custom function
// that takes a function ref to generate the url sent
var ds = new YAHOO.util.DataSource(url, {
responseType : YAHOO.util.DataSource.TYPE_JSON,
responseSchema : {
resultsList : 'Results',
fields : [ 'foo','bar','baz' ],
},
setInterval : unction(ms, reqBuilder, callback) {
var self = this,
intv = setInterval(function () {
self.makeConnection(reqBuilder(this.liveData), callback);
}, ms);
this._aIntervals.push(nId);
return nId;
}
});
// Create the DataTable
var dt = new YAHOO.widget.DataTable(tbl, cols, ds, {
paginator : new YAHOO.widget.Paginator({ rowsPerPage : 20 }),
dynamicData : true,
sortedBy : {key:"foo", dir:YAHOO.widget.DataTable.CLASS_ASC},
initialRequest : "sort=id&dir=asc&startIndex=0&results=25"
});
// To support server side pagination
dt.handleDataReturnPayload = function (req,res,payload) {
payload.totalRecords = res.meta.totalRecords;
return payload;
};
// custom augmentation, keeping an object reference with up to date state values for
// reference in the DataSource's interval
dt.liveState = dt.getState();
dt.dataSourceCallback = {
success : dt.onDataReturnSetRows,
scope : dt,
argument : dt.liveState
};
// Before each request is sent, update the state object so the callback passed along
// with the DataSource io reflects the current table state
ds.subscribe('requestEvent', function () {
YAHOO.lang.augmentObject(dt.liveState, dt.getState());
});
// initialize the interval, passing a function that will append the current state
// to the DataSource's seed url. In this case, we'll just use DataTable's default
// generateRequest method.
ds.setInterval(5000, function (url) {
return url + dt.get('generateRequest')(dt.liveState, dt);
}, dt.dataSourceCallback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment