$resource with parent-child variables

62 views
Skip to first unread message

Paul

unread,
Mar 12, 2013, 12:27:14 PM3/12/13
to ang...@googlegroups.com
I have a child resource -- image -- to a parent resource -- exhibit, and my RESTful api is currently set to /exhibit/:exhibitId/media/:mediaId for that image, and /exhibit/:exhibitId for the exhibit list.  I want to use $resource, if possible, to query the image from the exhibit, and I currently have this as my code:

angular.module('media',['ngResource']).
    factory('Image',function($resource){
        return $resource('/exhibit/:exhibitId/media/:mediaId',{},{update:{method:'PUT'},query:{method:'GET',isArray:false}});
});

I would wish that in my controller, I could just use Image.query(), and have it magically guess the exhibitId and insert it there, but that is obviously not possible.  Should I just extend my exhibit resource

angular.module('exhibit',['ngResource']).
    factory('Exhibit',function($resource){
        return $resource('/exhibit/:exhibitId',{},{update:{method:'PUT'},query:{method:'GET',isArray:false}});
});

to get the list of images, or is there something I'm missing?

Paul

unread,
Mar 12, 2013, 1:07:01 PM3/12/13
to ang...@googlegroups.com
Update:

I have decided to extend my exhibit variable to include the following:
angular.module('exhibit',['ngResource']).
    factory('Exhibit',function($resource){
        return $resource('/exhibit/:exhibitId',{},{
            update:{method:'PUT'},
            query:{method:'GET',isArray:false},
            images:{method:'GET',url:'/exhibit/:exhibitId/media',isArray:false}, // this is new
            find:{method:'GET',url:'/exhibit/find',isArray:false} /this is also new
        });
});

adding these two new functions to my exhibit resource, I did this:
var exhibit = Exhibit.find({url:$routeParams.exhibitUrl},function(){
            $scope.exhibit = Exhibit.images({exhibitId:exhibit.id});
        });

which now returns my images.  The problem here, however, is that to get an individual image without going through the mess of an array of them, I think that I must now extend the media module to include an exhibit Id.  perhaps I am going about this wrong.  Suggestions are welcome.
Reply all
Reply to author
Forward
0 new messages