Service results and where to put them

22 views
Skip to first unread message

John McPeek

unread,
Mar 18, 2014, 8:36:10 AM3/18/14
to ang...@googlegroups.com
Hi,
 I need a list of reports in a couple different places so I extracted the $resource into a service and inject the service. I hold the data internal to the service. I would like to attached the service to $scope in each controller so binding will work. Am I crazy or is that the "right" way?

Thanks,

John

Luke Kende

unread,
Mar 19, 2014, 2:49:21 AM3/19/14
to ang...@googlegroups.com
Yep, that's the right way.  I actually have a service called 'api' that defines all my $resource objects and then returns an object that references what I need the way I need it.  For example:

//very simplified from what I really have
 service('api', ['$http','$resource','$q',
        function($http, $resource, $q){
            var Account = $resource('/api/account',{},{ get: { method: 'get' } })
            var Settings = $resource('/api/settings',{},{ read: { method: 'get' }, save: { method: 'put'} })
            var Reports = $resource('/api/reports/:reportName',{ reportName: '@ reportName' },{ loadReport: { method: 'get' }, update: { method: 'put'} })

            return {
                account: new Account(),
                settings: new Settings(),
                singleReport: function(){
                    return new Reports(); //case where I need an api resource for managing a report that doesn't save state across controllers it's injected into
                }
            }
        }
])

//sample controller
function someController($scope, api){
   $scope.settings = api.settings;
   $scope.settings.$read( function success(dataReturned) { 
        console.log( 'is dataReturned really the same things as $scope.settings? ', dataReturned == $scope.settings); //true  
   });

   $scope.myReport = api.singleReport();
   $scope.myReport.$loadReport({{ reportName: 'gross-sales' }) //will call /api/reports/gross-sales and bind result to scope
}

Now my settings template in html will interpolate as soon as the api returns the result and the same settings will be anywhere I inject the service without needing to call $read again.  There's even more magic you can do, but this is a good start for now... good job getting this far and questioning yourself, you are on the right path.

Sander Elias

unread,
Mar 19, 2014, 3:08:35 AM3/19/14
to ang...@googlegroups.com
Hi John,

That is indeed the right way. However, if those controllers are all in the same part of the application, you can consider placing an outer/group controller around this part.
I just created this example on what can be done with scope inheritance. I suspect you might find this usefull too!

Regards
Sander

John McPeek

unread,
Mar 19, 2014, 6:25:25 AM3/19/14
to ang...@googlegroups.com
WOW, thanks for the replies. Very helpful.


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/cDWePgJzIFI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages