0

Sorry for the pseudo code, but I can't post the original code here(company policy).

I have two "classes" in javascript, the class CLS2 I use to populate a DIV with HTML which come from the server (Actually it's a table). I am using the method LOAD in jquery for this.

The class CLS1 calls the CLS2 to load the HTML table and after trying to get some infos from the table with jquery method FIND.

I know what is happening. When I try to get these infos inside the table I am not able to do this, because the HTML table is not ready yet.

But if I am using the jquery method WHEN, I guessed, I should be able to do this. But it is not working so far.

If I use the callback on the jquery LOAD method everything works fine, but when I am using WHEN it is not waiting the HTML table to get ready.

Am I forgetting any thing?

I have a return from the jquery LOAD method. Is it possible to check in this return if the content is ready? console.log(retParam);

CLS2:

var cls2 = (function ($) {

    return { 

        load: function() {

            return this.populateDiv();

        },


        populateDiv: function() {

            var ret = $('#content').load('url', function(){

                // IF I USE THE JQUERY METHOD FIND HERE WORKS FINE

            });

            return ret;

        }

    };

}(jQuery));

CLS1:

var cls1 = (function ($) {

    return { 

        init: function(){

            this.config();

        },

        config: function() {

            $.when(cls2.load()).then(this.doPagination());

        },


        doPagination: function(retParam) {

            console.log(retParam);

            var val = $('#datatable').find('.tdActive').val();

        }

    };

}(jQuery));

JQUERY Ready

$(document).ready(){

    cls1.init();

}
4

1 回答 1

2

加载返回jQuery,而不是承诺。如果您改为.load()替换.get(),因为 .get() 返回一个您可以在 .when on 时执行的承诺(实际上是一个 jqXhr)。

就像是

var ret = $.get( "url", function( data ) {
  $( '#content' ).html( data );
});

return ret;
于 2013-10-03T20:32:00.747 回答