Friday, October 17, 2014

I need AngularJS service instances, not singletons

One of the apps we're working with has a bunch of WebApi controllers and a bunch of AngularJS controller talking to them.

So we've got the backend URLs like:
- /api/typeA
- /api/typeB
- /api/typeC
- /api/typeD
- /api/typeE

Yes, we could bundle all the types in one WebApi, but then the object would become quite big and the saving part would require more logic. So we've got separate controllers for every type.

Most controllers only implement GET and POST and we were discussing an elegant way to get that into our app. There were a couple of options:
- Create a Angular service for every WebApi controller
- Create one service that takes the URL with every call
- Do something else ...

AngularJS only provides singletons ( link ) but I would really love to create a service instance that talks to a specific WebApi controller. Therefore I've sent a POC over to my colleague in which I suggest to have Angular inject a factory ( which is a singleton ), which is then able to build instances for each of the WebAPI controllers.

Here's my fiddle: http://jsfiddle.net/jochenvanwylick/0qcwver5/1/


No comments:

Post a Comment