YUI 3.x Home -

YUI Library Examples: DataSource Utility: DataSource with Polling

DataSource Utility: DataSource with Polling

DataSource's Pollable extension enables polling functionality on all your DataSource instances.
Poll every second for current time:

Include the datasource-pollable extension in your Y.use() statement to add the setInterval(), clearInterval(), and clearAllInterval() methods to all your DataSource instances.

  1. YUI().use("datasource-function", "datasource-polling", function(Y) {
  2. var myFunction = function() {
  3. return new Date();
  4. },
  5. myDataSource = new Y.DataSource.Function({source:myFunction}),
  6. request = {
  7. callback: {
  8. success: function(e){
  9. Y.one("#demo_output_polling")
  10. .setContent("At the tone the time will be: " +
  11. Y.dump(e.response.results[0].toString()));
  12. },
  13. failure: function(e){
  14. Y.one("#demo_output_polling")
  15. .setContent("Could not retrieve data: " + e.error.message);
  16. }
  17. }
  18. },
  19. id = myDataSource.setInterval(1000, request); // Starts polling
  20.  
  21. myDataSource.clearInterval(id); // Ends polling
  22. });
YUI().use("datasource-function", "datasource-polling", function(Y) {
    var myFunction = function() {
            return new Date();
        },
        myDataSource = new Y.DataSource.Function({source:myFunction}),
        request = {
            callback: {
                success: function(e){
                    Y.one("#demo_output_polling")
                     .setContent("At the tone the time will be: " +
                                Y.dump(e.response.results[0].toString()));
                },
                failure: function(e){
                    Y.one("#demo_output_polling")
                     .setContent("Could not retrieve data: " + e.error.message);
                }
            }
        },
        id = myDataSource.setInterval(1000, request); // Starts polling
 
        myDataSource.clearInterval(id); // Ends polling
});

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings