Simcha
Simcha

Reputation: 3400

OOP approach in js with ajax

... Hi I have an object:

function Page(daoService) {
    this.daoService = daoService;
    this.gamesList = this.daoService.getGamesList();
}

// Rendering thumbs for main page
Page.prototype.renderThumbs = function(idContainer){
    var container = document.getElementById(idContainer);
    for (var i = 0; i < this.gamesList.length; i++) {
        var thumbNode = document.createTextNode("<div class='thumbIcon'></div>");
        thumbNode.style.backgroundImage = Const.PATH_THUMBS + this.gamesList.gameTitleseo;
        container.appendChild(thumbNode);
    }
};

and also I have function in my code that uses this object:

document.onreadystatechange = function() { 
    if (document.readyState == "interactive") { 
        // Initialization of elements
        // Setting global dao service
        var daoService = AjaxService();

        // Initialization for page object
        var page = new Page(daoService);
        page.renderThumbs("homePageContainer");
    } 
} 

The issue here that when I'm calling page.renderThumbs, field this.gamesList still didn't initialized, because it didn't get ajax response from server. Can you help me to handle with it what I need to change in my approach? Thanks

Upvotes: 3

Views: 534

Answers (2)

Hayes
Hayes

Reputation: 858

You'll need to set getGamesList on daoService to handle an asynchronous method. Here is a rough sketch of what I'm meaning:

DaoService.prototype.getGamesList = function(callback) {

    var self = this;

    // Has the gamesList been populated by a previous call to this function?
    if (this.gamesList) {
        // The gamesList property has values, call the callback.
        callback(this.gamesList);
    }
    else {
        // The gamesList was not populated, make the ajax call.
        $.ajax('URL').done(function(data) {
            // Handle data coming back from the server.
            self.gamesList = data;
            callback(this.gamesList);
        });
    }


}

You can then consume the getGamesList method call from renderThumbs like so:

Page.prototype.renderThumbs = function(idContainer){
    var container = document.getElementById(idContainer);

    // The anonymous function will be called whether the list was
    // already populated in daoService or an ajax call is made.
    this.daoService.getGamesList(function(gamesList) {
        for (var i = 0; i < gamesList.length; i++) {
            var thumbNode = document.createTextNode("<div class='thumbIcon'></div>");
            thumbNode.style.backgroundImage = Const.PATH_THUMBS + gamesList[i].gameTitleseo;
            container.appendChild(thumbNode);
        }
    });
};

Upvotes: 3

Peter Aron Zentai
Peter Aron Zentai

Reputation: 11740

Use promises in your daoService when returning asyn results. jQuery has a nice promise framework Symbolick code:

function DaoService() {
   this.getGamesList = function() {
       return $.Deferred(function(defer) {
            //Do ajax request here and wait for callback/complete
            //replace DoAjax to your ajax or better yet use jQuery ajax that is already promise aware
            DoAjax(function onReady(result) {
                defer.resolve(result.data);    
            });

       }).promise();
   }
}

In your page class whenever you need the games list call it as shown below. It will only download data once, every subsequent time it'll use the data already downloaded.

Page.prototype.renderThumbs = function(idContainer){
    var container = document.getElementById(idContainer);
    this.daoService.getGamesList().then(function(gamesList) {
       for (var i = 0; i < gamesList.length; i++) {
          var gameListItem = gamesList[i];
          //do your funky html concats or use some templating
       });
    });
    }
};

Upvotes: 2

Related Questions