加载类异步的单个属性

Load single property of a class async

本文关键字:单个 属性 异步 加载      更新时间:2023-09-26

我正在构建一个动态UI,我有一个"ul"列表,需要根据"li"从数据库加载单个值。一种显示数据库中计数的徽章。

以下是一些代码:

function A {
    var count;
    function buildItem() {
        //some code to build a list "li" item
    };
    this.setCount = function(number) {
        count = number;
    };
    this.getHtml = function() {
        buildItem();
        return html;
    };
};
function B {
    var ul;
    var container = [];
    function loadCount(ref, i) {
        //ajax call that should set count for class A
        $.ajax({
            //blabla
            container[i].setCount(jsonResult);
        });
    };
    this.buildList = function() {
        //code that build the list..
    };
};

我需要的是loadCount()在解析脚本之前或同时分配从数据库检索的值。使用异步请求在显示列表后填充计数。

如何使用从数据库获取的值填充我的 A 类计数,并在显示整个列表后填充?

隐藏ul,直到收到所有li元素的计数。在加载期间显示加载程序。

var nResponses = 0;
$.ajax( "example.php" )
  .done(function(msg) {
     addLi(msg);                   //Add an li element to the ui
     if (liCount === nResponses)   //Check if all the responses are received. liCount is the number of lis you have
       showUl();                   //Everything is good. Show the ui  
     else
       nResponses++;               //Increase the response count  
  })