R
R
RodgerFox2015-11-16 21:19:19
Angular
RodgerFox, 2015-11-16 21:19:19

Sequentially executing queries using $resource?

Greetings!
And so, we have a factory:

app.factory('SettingProject', function($rootScope, $resource) {
    return $resource($rootScope.storeUrl+'/settingProject&token='+$rootScope.token, {
        id_setting: '@id',
        setting_type: 'main',
        setting_profiles: 'all'
    }, 
    {
        'profiles': {
            method: 'GET', params: {setting_profiles: 'all'}, isArray: true
        },
        'profile': {
            method: 'GET'
        }
    });
});

This is how we call them in the controller:
// создаем запросы в контроллере
$scope.listProfiles = SettingProject.profiles({setting_profiles: 'all'});
$scope.listProfile = SettingProject.profile({id_setting: $routeParams.id_setting, setting_profiles: 'check'});

Everything is simple, until you start to think why the first request does not work correctly, it's all about the passed setting_profiles: 'all' value, the request is sent to the controller, which in turn to the model, and in it, for some reason, one value is used for two requests variable setting_profiles and it is check !
What is the correct way to force requests to be executed sequentially and not asynchronously?
The idea of ​​using, for example, .promise and then calling a second request is a bad idea, because these requests are independent of each other, so it is assumed that there is no need for them to depend.
Tell me plz.
Note from Spanish. .$promise:
$scope.listProfiles = SettingProject.profiles({setting_profiles: 'all'}).$promise
        .then(function(data) {
            console.log($scope.listProfiles);
        }, function(error) {
            console.log(error);
        });

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question