2

我的控制器中有以下功能:

$scope.model.listApplicantStatuses = function(){
    var statusChoices = jobsService.getApplicantStatuses();

    if(statusChoices !== null)
            return statusChoices;

    //if($scope.model.listApplicantStatuses.inProgress)
    //  return;
    //$scope.model.listApplicantStatuses.inProgress = true;
    jobsService.fetchApplicantStatuses().then(function(data){
        jobsService.setApplicantStatuses(data.data);
        return data.data;
    },
    function(data){
        $scope.layout.showNotification('error', 10 * 1000, 'we are is experiencing technical difficulties <a href="mailto:support@" class="btn btn-danger">Contact Support</a>');
    });
}

对应服务代码:

jobsServ.fetchApplicantStatuses = function(){
    return $http.get(utils.getBaseUrl() + '/applications/status_choices', utils.getConfig());
}
jobsServ.getApplicantStatuses = function(){
    return that.applicantStatusChoices;
},
jobsServ.setApplicantStatuses = function(choices){
    that.applicantStatusChoices = choices;
},

DOM 用法示例:

<select class="statusSelect" data-ng-model="model.editedApplicant[applicant.id].status" data-ng-show="layout.statusVisible(applicant.id) && !layout.statusLoader[applicant.id]" data-ng-options="key as val for (key, val) in model.listApplicantStatuses()" data-ng-change="model.updateStatus(applicant.id)"></select>

现在,我遇到的问题是,虽然 chrome 一直等到第一个函数调用完成,然后给我从 AJAX 调用中获得的数据,并同时返回 undefined,但 Firefox 一遍又一遍地调用该函数,创建大量不需要的 XHR 请求。

我注释掉了设置 inProgress $scope 变量的代码,因为它对我来说似乎是一个 jQurish 解决方案,并且因为这将迫使我在许多地方更改我的代码,并创建另一个布尔标志,例如这个 per对服务器的每个请求。

4

2 回答 2

2

我希望 Firefox 的行为。也许您应该将逻辑更改为:

// a variable to keep statuses, or a promise
$scope.applicantStatusList = $scope.model.listApplicantStatuses();

更改listApplicantStatuses()以返回承诺:

$scope.model.listApplicantStatuses = function() {
    var statusChoices = jobsService.getApplicantStatuses();

    if(statusChoices !== null)
        return statusChoices;

    return jobsService.fetchApplicantStatuses().then(function(data){
        jobsService.setApplicantStatuses(data.data);
        return data.data;
    },
    function(data){
        $scope.layout.showNotification(...);
    });
};

并在applicantStatusList中使用<select>

<select ... data-ng-options="key as val for (key, val) in applicantStatusList" ...></select>
于 2013-10-03T12:04:16.007 回答
1

我通过在发送 xhr 的函数中分配一个名为 _root 的局部变量来解决这个问题,在发送请求后将 _root.ingProgress 设置为 true,并在收到答案后将其切换为 false。

奇迹般有效。

代码:

$scope.model.listApplicantStatuses = function(){
    var statusChoices = jobsService.getApplicantStatuses();

    if(statusChoices !== null)
            return statusChoices;
    var _root = this;
    if(_root.inProgress)
        return;

    _root.inProgress = true;
    jobsService.fetchApplicantStatuses().then(function(data){
        jobsService.setApplicantStatuses(data.data);
        _root.inProgress = false;
        return data.data;
    },
    function(data){
        _root.inProgress = false;
        $scope.layout.showNotification('error', 10 * 1000, 'We are currently experiencing technical difficulties <a href="mailto:support@" class="btn btn-danger">Contact Support</a>');
    });
}
于 2013-10-03T12:42:28.537 回答