【问题标题】:How to make sure that a function is called only after an object is build?如何确保仅在构建对象后才调用函数?
【发布时间】:2021-03-19 16:58:10
【问题描述】:

我有一个common service.js,它具有通用功能,但我正在使用和创建的功能如下:

this.createFastlookupDictionaryForEmployee = function (data) {
        --code to build dictionary
        return dictionaryArr;
    }
    
    this.getEmployeeId = function (dictionaryArr,employeeId) {
        return dictionaryArr[employeeId].
    }

控制器

function onPageLoad(Callback1,
                Callback2,
                Callback3) {
                myService.getEmloyeeList().success(function (data) {
                    $scope.employeeLookupList = commonService.createFastlookupDictionaryForEmployee(data.items);
                    Callback1();
                    Callback2();
                }).finally(function () {
                });
            }
            
  $scope.getEmployeeId = function () { //callback function 1
                myService.getEmployeeData().success(function (data) {
                    var employeeId = $scope.employeeLookupList[10]; //error here as the $scope.employeeLookupList is not populated yet
                }).finally(function () {
                });
            };

所有回调函数都在访问来自$scope.employeeLookupList 数组对象的数据。现在的问题是由于getEmloyeeList API 调用返回大量数据,这就是为什么构建字典需要时间,回调函数调用已完成,因此无法读取某些员工 ID。

我无法将回调传递给createFastlookupDictionaryForEmployee 方法,因为它在很多地方都被使用。

我如何确保字典已创建并且只有在此之后我的 Callback1, Callback2 才会被调用?

我不想在填充字典之前调用 Callback1 和 Callback2。

【问题讨论】:

    标签: javascript angularjs callback


    【解决方案1】:

    您应该像第一次服务myService.getEmloyeeList() 一样拨打您的Callback's。话虽如此,您应该像这样使用success / finally 等待它的响应:

    this.createFastlookupDictionaryForEmployee = function (data) {
            var defer = $q.defer();
            // code to build dictionary
    
            // after dictionary been resolved
            defer.resolve(dictionaryArr);
    
            // if you need to throw an error you can use it like
            // defer.reject("error");
    
            return defer.promise;
        }
    
    myService.getEmloyeeList().success(function (data) {
     $scope.employeeLookupList = [];
     commonService.createFastlookupDictionaryForEmployee(data.items).then(function (data) {
               $scope.employeeLookupList = data.items; //response from service
               // Do something when request comes with success response
               // I think you should then receive the data on your callback's
               Callback1(data.items);
               Callback2(data.items);
          })['catch'](function (resp) {
             //Do something after error responses
          })['finally'](function () {
             //Do something after success or error responses
          });
    }).finally(function () {
    });
    

    这样Callback's 函数只会在使用请求中的数据成功时被调用。在此之后,您可以观察$scope.employeeLookupList 以检查它是否为空。 快乐编码:)

    【讨论】:

    • createFastlookupDictionaryForEmployee 不在 API 调用中,为什么我们将其封装在 ".success" 中?
    • 我在“commonService.createFastlookupDictionaryForEmployee(data.items).success”上遇到错误,说“这不是一个函数”
    • 就像在您的示例中您确实在成功中使用它一样,您的服务 commonService 是否应该有一个服务 createFastlookupDictionaryForEmployee(data.items)?您的请求需要有一个 http 调度程序,以便您可以使用 .sucess 或 finally
    • 但是 createFastlookupDictionaryForEmployee(data.items) 是一个普通的 javascript 函数,因此它没有 HTTP 调度程序。 createFastlookupDictionaryForEmployee(data.items) 方法与我在问题中发布的方法完全相同
    • 哦,我现在确实没明白。检查我的编辑。 ['catch] 和 ['finally'] 可能会因 angularjs 版本而异
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2019-03-22
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    相关资源
    最近更新 更多