0

In this function I am retrieving every album from a list of albums:

options.albumsList.forEach(function (id) {
    var promise = $resource('https://api.imgur.com/3/album/:album',{album:id},{
        get:{
            headers:{'Authorization':'Client-ID '+ options.apiKey},
            method:'GET'
        }        
    }).get().$then(
        function(value){
            albums.push(value);
        },
        function(err){
            console.log(err);
        }
    );
    prom.push(promise);
});
$q.all(prom).then(function () {
    console.log(albums);
});

What you will notice is :album is dependant on each id in albumList

This means if I wanted to assign the resource call to a reusable object like:

var call = $resource('https://api.imgur.com/3/album/:album',{album:id},{
        get:{
            headers:{'Authorization':'Client-ID '+ options.apiKey},
            method:'GET'
        }        
    });

And then wrap it with:

options.albumsList.forEach(function (album) {
    //Can no longer pass album id to each call
    var promise = call.get().$then(
        function(value){
            albums.push(value);
        },
        function(err){
            console.log(err);
        }
    );
    prom.push(promise);
});
$q.all(prom).then(function () {
    console.log(albums);
});

I can no longer pass the album id to each :album

Is there a way around this?

1 Answer 1

1

From the resource documentation (scroll down to the Return section), looks like you can just pass in a parameter to get that will replace the value in the URL.

var promise = call.get({album: album.id})

Sign up to request clarification or add additional context in comments.

2 Comments

I can't believe I missed that. Thankyou. Is the same thing acheivable with $http as well?
Since $http comes from $q, I think so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.