AjaxModule - Voliware/AjaxPlus GitHub Wiki

Give Me Data or Give Me Death

If your web app talks like this, it might need an AjaxModule. This simple little module will make requests to the backend at a defined interval. It is easily extendable, and recommended to do so.

If used as a simple object, your app can listen to the same-named jQuery AJAX events, done, fail, and always.

var scoreboard = new AjaxModule({
   request : Server.getLatestScores,
   interval : 25000
})
.on('done', function(data){
   app.updateScoreBoard(data);
})
.on('fail', function(){
   app.showSyncError();
});

If extended as a class, take advantage of the built in callbacks and overridable data processing.

class ScoreBoard extends AjaxModule {
   constructor(){
      // instead of a defined request function,
      // you could pass in options for $.ajax
      super({
         url : '/score/latest/',
         data : {teams:'all'}
      }); 
      // renders the scores
      this.displayBoard = new DisplayBoard();
      return this;
   }

   // done callback
   _done(data){
      // data will always be processed by _cacheData and _processData
      this.displayBoard.render(this._processedData);
   }

   _processData(data){
      this._processedData = super._processData(data);
      this._processedData.mapleLeafs += 2;
      this._processedData.habs = 0;
      return this;
   }
}