0

我试图在代码下面运行,但它抛出错误。似乎在handlebar.compile中我传递了null(a [j]),我认为这是因为控制没有进入.when的功能,因为我没有看到任何登录控制台。我可能错了。但请帮我纠正它。

代码:

 if (pageName.page === "dashboard") {
      $.getJSON('api/filterTemplate/dashboard',function (data){
           var a = new Array(data.length);
           console.log(data);

           $.when(function(){
                console.log("inside function 1");
                for (var i = 0; i < data.length;) {
                     console.log("inside funct 1 for loop");
                     $.get('commonCore/templates/' + data[i].templateHtml , function(html) {
                          a[i]=html;
                          i++;
                          console.log("inside funct 2" + i +"  " + html);
                     });
                }
           }).then(function(d) {
                for (var j = 0;j < data.length; j++) {
                     filterTemplate = Handlebars.compile(a[j]);
                     replaceFilterTemplate(data[i].classids);
                }
           });
      });
 }

JSON:

[
    {
        "templateHtml": "dashBoardLeftInsight.html",
        "classSelect": "leftpanel"
    },
    {
        "templateHtml": "dcdcsFilterOptions.html",
        "classSelect": "dcdcsOptions"
    },
    {
        "templateHtml": "advanceFilterOptions.html",
        "classSelect": "advancedOptions"
    },
    {
        "templateHtml": "reportFilterOptions.html",
        "classSelect": "reportdashboard"
    }
]
4

2 回答 2

0

$.when期望一个 Promise 对象作为其参数,如果传递了一个非 Promise,那么它将假定它是一个已解析的对象并调用成功回调。

在您的情况下,您将传递一个函数作为参数,该函数将在实际的 ajax 请求完成之前触发成功回调。

尝试

if (pageName.page === "dashboard") {
    $.getJSON('api/filterTemplate/dashboard', function (data) {

        var requests = $.map(data, function (rec, i) {
            return $.get('commonCore/templates/' + rec.templateHtml, function (html) {
                console.log("inside funct 2" + i + "  " + html);
            });
        });

        $.when.apply($, requests).then(function () {
            return $.map(arguments, function (args) {
                return args[0]
            });
        }).done(function (responses) {
            $.each(responses, function (i, response) {
                filterTemplate = Handlebars.compile(response);

                replaceFilterTemplate(data[i].classids);
            })
        })
    });
}

PoC:小提琴

于 2013-11-15T09:21:41.707 回答
0

我相信您的子请求与使用它们的处理程序函数不同步。我会尝试这样的事情:

if (pageName.page === "dashboard") {
  $.getJSON('api/filterTemplate/dashboard',function (data){
       var a = new Array(data.length);
       console.log(data);
       var allRequests = [];
            for (var i = 0; i < data.length;) {
                 console.log("inside funct 1 for loop");
                var request =  $.get('commonCore/templates/' + data[i].templateHtml , function(html) {
                      a[i]=html;
                      i++;
                      console.log("inside funct 2" + i +"  " + html);
                 });
             allrequests.push(request);
            }



       $.when.apply($, allRequests).then(function(d) {
            for (var j = 0;j < data.length; j++) {
                 filterTemplate = Handlebars.compile(a[j]);
                 replaceFilterTemplate(data[i].classids);
            }
       });
  });

}

于 2013-11-15T09:29:53.573 回答