insights on recurring controller/service/api relationship

30 views
Skip to first unread message

Jonathan Price

unread,
Jan 20, 2015, 5:39:37 PM1/20/15
to ang...@googlegroups.com
I’m finding this kind of relationship in my apps a lot, and I’d love some critiques/warnings/best practices ideas from you folks.  Kinda made it up as I went along, and now I’m wondering if it’s the best way?  I’ve left out some functionality (like update, delete, find individual thing, etc) from the service,  but I wanted to simplify the idea to make it easier for you to respond.  Thanks for the insight.

controller:

.controller(’ThingController', ['$scope', ‘ThingService’,
    function($scope, ThingService) {

    $scope.model = {};   
    $scope.model.things = ThingService.getThings();

    ThingService.loadThings();

    $scope.create = function() {
        ThingService.create();
        ThingService.ready().then(
            function() {$scope.model.things = ThingService.getThings(); } //refresh array
        }
    }

}])

service:
    .factory(’ThingService', ['$q', ‘api-service',
        function($q, apiSvc) {

        var model = {};
        var loaded = $q.defer();
       
        model.things = [];

        return {
            loadThings: loadThings,

            ready: function() {
                return loaded.promise;
            },

            getThings: function() {
                return model.things;
            },

            create: function(thing) {
                loaded = $q.defer();
                apiSvc.createThing(thing).then(function(result) {
                    model.things.push(result.data);
                    resolve();                   
                });
            }
        };

        function loadThings(userID) {
            loaded = $q.defer();
            apiSvc.getThings(userID).then(function(result) {
                angular.copy(result.data, model.things);
                resolve();
            });
        }

        function resolve() {
            return loaded.resolve()
        }
}])

api-service:
    .factory('APIService', ['$http’,  function($http) {
        getThings: function(credentials) {
            return $http({
                method: “get",
                url: ‘http://website.com/getThings'
            })
        }
}])

Sander Elias

unread,
Jan 21, 2015, 1:32:03 AM1/21/15
to ang...@googlegroups.com
Hi Jonathan,

This is similar to what $resource does, with the difference that $resource abstracts it even more. I'm not suggesting that you need to move to $resource, but rather that you read trough the docs and source of it. It will give you the pointers you need to make your own implementation a bit more versatile, so you don't need to repeat it for every thingieTM you need to load.

Regards
Sander

Puritan Paul

unread,
Jan 21, 2015, 5:29:00 PM1/21/15
to ang...@googlegroups.com
I’ll definitely do some more reading.  I’d started to dabble with resource, but I’ve had a little trouble understanding the right way to use it.  Seems like the right track, though.

Thanks.


--
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/NBJ5lQ4ln4k/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.

Sander Elias

unread,
Jan 22, 2015, 1:22:25 AM1/22/15
to ang...@googlegroups.com
Hi Jonathan,

I don't use $resource often. I do build my own versions of it (as you are on pathway to do the same!). Rolling my own has the upside that I have more control over how the communication with the server is done. In some cases, I don't even use $http, but web-sockets for example. Currently I'm experimenting (and liking) with RxJS, wich has it's own ajax commands. By rolling your own resource you gain the freedom to control the way your app communicates with your back-end  without much hassle!

Regards
Sander
Reply all
Reply to author
Forward
0 new messages