Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Created May 8, 2013 15:17
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 juandopazo/5541166 to your computer and use it in GitHub Desktop.
Save juandopazo/5541166 to your computer and use it in GitHub Desktop.
DataTableDataSource polling
YUI().use("datatable", "datasource-io", "datasource-jsonschema", "datatable-datasource", "datasource-polling", function (Y) {
Y.mix(Y.Plugin.DataTableDataSource.prototype, {
initializer: function (config) {
this.after('datasourceChange', this._bindDataSource);
if (config.datasource) {
this._bindDataSource({
newVal: config.datasource
});
}
if (typeof config.initialRequest !== 'undefined') {
this.load({
request: config.initialRequest
});
}
},
destructor: function () {
if (this._dsHandler) {
this._dsHandler.detach();
this._dsHandler = null;
}
},
_bindDataSource: function (e) {
if (this._dsHandler) {
this._dsHandler.detach();
this._dsHandler = null;
}
if (e.newVal) {
this._dsHandler = e.newVal.on('response', this.onDataReturnInitializeTable, this);
}
},
load: function (config) {
config = config || {};
config.request = config.request || this.get("initialRequest");
if (config.datasource) {
this.set('datasource', config.datasource);
}
this.get('datasource').sendRequest(config);
}
}, true);
var url = "/ukotir/1/js/",
dataSource,
table;
dataSource = new Y.DataSource.IO({
source: url
});
dataSource.plug(Y.Plugin.DataSourceJSONSchema, {
schema: {
resultListLocator: "query.results.Result",
resultFields: [
"Title",
"Phone",
{
key: "Rating",
locator: "Rating.AverageRating",
parser: function (val) {
return Math.floor(Math.random() * 10) + '';
}
}
]
}
});
table = new Y.DataTable({
columns: [
"Title",
"Phone",
{
key: "Rating",
formatter: function (o) {
if (o.value === -1) {
o.value = '(none)';
}
}
}
],
summary: "Pizza places near 98089",
caption: "Table with JSON data"
});
table.plug(Y.Plugin.DataTableDataSource, {
datasource: dataSource
});
table.render("#pizza");
dataSource.setInterval(1000, {
request: ''
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment