0

The following code works, and I get the JSON below returned. What I'm interested in is the cars_url. How can I get the URL (and what is the best way) then make a secondary get request?

JSON

{
    created: "2013-11-08T18:57:44",
    domain: "www.test.com",
    cars_url: "/api/v1/carlot/4",
    image: null,
    modified: "2013-11-08T18:57:44"
}

JavaScript

app.factory('CbgenRestangular', function(Restangular) {
    return Restangular.withConfig(function(RestangularConfigurer) {
        RestangularConfigurer.setBaseUrl('http://127.0.0.1:8000');
    });
});


app.controller("CampaignData" ,
               [
                   '$scope',
                   'Restangular',
                   'CbgenRestangular',
                   function($scope, Restangular, CbgenRestangular){
                       CbgenRestangular.one('api/v1/person', "james").get().then(
                           function(person) {
                               $scope.person = person;
                               console.log(person)
                           }
                       );
                   }
               ]);

2 Answers 2

1
app.controller("CampaignData" , ['$scope', 'Restangular', 'CbgenRestangular', '$http',
    function($scope, Restangular, CbgenRestangular, $http){

        CbgenRestangular.one('api/v1/person', "james").get().then(function(person) {
                $scope.person = person;
                console.log(person);
                var cars = $http({method: 'GET', url:person.cars_url);
                cars.then(function(data){
                     // do success things here
                }, function(data){
                     /do error things here
                });
            }
        );

}]);

Nesting the requests can get messy if you have more than one level deep. In that case, you should use $q to control the flow of the requests.

app.controller("CampaignData" , ['$scope', 'Restangular', 'CbgenRestangular', '$http', '$q',
    function($scope, Restangular, CbgenRestangular, $http, $q){
    $scope.person = {cars_url:"your 404 url here"};

    var personcall = CbgenRestangular.one('api/v1/person', "james").get();
    $q.when(personcall.then(
        function(person) {
            $scope.person = person;
            console.log(person);
        }
    ))
    .then(function(){
        var cars = $http({method: 'GET', url:$scope.person.cars_url);
        cars.then(function(data){
             // do success things here
        }, function(data){
             /do error things here
        });
    });
}]);
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to do it any time $scope.person changes (which happens in the response from the person request), you could set up a $watch in your controller.

$scope.$watch('person', function(newPerson, oldPerson){
    // Ignore Angular's initial broadcast
    if(!newPerson){
        return false;
    }
    CbgenRestangular.one(newPerson.cars_url).get().then(function(data){
        // Deal with cars data
    });
});

Comments

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.