javascript - AngularJS退出递归函数调用

标签 javascript angularjs recursion ionic-framework

我使用以下函数从 REST API 获取用户,按偏移量分页。 在成功回调上,该函数会使用新的偏移量再次递归调用,以获取下一个垃圾用户。

问题:如果我切换或离开 View ,FetchAttendee-Function 将运行,直到获取所有用户。但是,为了提高性能,我想停止为用户获取数据。

fetchAttendees(event_id, offset);

function fetchAttendees(event_id, offset) {
    AttendeeFactory(offset).show({id: event_id}, 
        function success(response) {
            [ DO SOMETHING WITH RESPONSE ]
            fetchAttendees(event_id, offset);
        }, 
        function (error) {
        });
}

那么,是否可以停止在查看离开事件上调用fetchAttendee-Function

$scope.$on("$ionicView.leave", function(scopes, states) {
    [ ...] 
});

AttendeeFactory

.factory('AttendeeFactory', function ($resource) {
    return function (offset) {
        return $resource('http://10.0.0.6:8000/backend/attendees/:id/', {}, {
            show: { method: 'GET', headers: {'attendee-offset': offset}, isArray: true }
        });
    };
})

最佳答案

这是伪代码(未经测试您想要做什么)

// in your controller
app.controller('YourController', ['$scope', 'AttendeeFactory', function($scope, AttendeeFactory) {

    ...
    AttendeeFactory.fetchAttendees(event_id, offset);
    ...

}]);



// in the state change handler that matches leaving your view
AttendeeFactory.pause();


// in your service/factory
app.factory('AttendeeFactory', function($resource) {
    var isPaused = true; 

    function fetchAttendees(event_id, offset) {
        isPaused = false;
        fetchAttendeesRecursive(event_id, offset);
    }

    function fetchAttendeesRecursive(event_id, offset) {
        if (!isPaused) {
            Attendee(offset).show(
                {id: event_id}, 
                function success(response) {
                    [ DO SOMETHING WITH RESPONSE ]
                    fetchAttendees(event_id, offset);
                }, 
                function (error) {}
           );
        }
    }

    function Attendee(offset) {
        return = $resource(
            'http://10.0.0.6:8000/backend/attendees/:id/',
            {},
            {
                show: {
                    method: 'GET', 
                    headers: {'attendee-offset': offset}, 
                    isArray: true
                }
            }
        );
    }

    function pause() { isPaused = true; }

    return {
        fetchAttendees: fetchAttendees,
        pause: pause
    };
});

如果 [DO SOMETHING WITH RESPONSE ] 包括将其绑定(bind)到 View 的范围,那么您必须添加代码以使服务通知 Controller 数据已更改。

在这种情况下,您可以使用 $rootScope、$on 和 $emit 在获取与会者时从服务发出一条消息,以便 Controller 可以监听并更新。这是一个简单的例子:

// in the controller
$rootScope.$on("AttendeeFetchedEvent", function($event, data){
  // do something with data
});

// in the factory/service
$scope.$emit("AttendeeFetchedEvent", dataToSendToController);

关于javascript - AngularJS退出递归函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31659057/

相关文章:

javascript - Controller 已加载到 DOM 中,但 View 未加载且无法找到 Controller - oclazyload with jade(pugjs)

visual-studio-2010 - 递归方法

javascript - 如何从 Next.js 中的 Formik 提交返回 API(获取)数据

javascript - 如何用angularjs检测混合内容?

javascript - x = function(a, b, c){} 和 function x(a, b, c){} 有什么区别?

javascript - 在 'this' 上指定对象与在函数中返回对象本身之间的区别

c++ - 具有递归函数的二进制转换给出奇数

python - 递归地对列表的元素求和

javascript - 将 C# 自定义数组传递给 JavaScript

javascript - 是否有任何使用 Node.js 的 Phonegap API 的实现?