1

For a project I am working on, one of the things I've written is an instantiable service for an Angular UI Bootstrap Calendar control.

You can see the plunker for it in action here.

The code question I have is more of an architectural and best-practices question. Specifically, I think I've written an Angular anti-pattern.

Services - like the calendarSvc - are singletons, yet I am explicitly circumventing this by making the factory return a constructor function.

That being said, there is a concrete business need for 1-n calendars to exist on a single page. This code is an effective refactor of the code needed to manage a single calendar, so it definitely helps the code to be more DRY.

Question: What are some effective alternatives to this instantiable service that still let me specify instances of reusable Calendar objects as needed, but are done without circumventing the Angular way of managing the code?

Andrew Gray
  • 3,756
  • 3
  • 39
  • 75
  • Possibly a better fit for codeReview or Programmers http://codereview.stackexchange.com , http://programmers.stackexchange.com/ – mccainz Oct 22 '14 at 14:06
  • While the moderators can do what they like with my question, I don't think it's a code review question, considering it's me questioning my own code. I'm not sure that even qualifies as a proper review. As far as whether it belongs on the Programmers stack, there's a lot of stuff on StackOverflow that could be considered to belong there. To be honest, I'm not sure where StackOverflow begins, and the Programmers stack properly begins. – Andrew Gray Oct 22 '14 at 14:16
  • What you did is not a antipattern, but indeed best practice! See also this page for example: https://github.com/mgechev/angularjs-style-guide#services. The idea of the factory is the possibility to return constructor functions. – DanEEStar Oct 22 '14 at 14:17
  • @DanEEStar If you believe an answer is that I am correct, feel free to make that an answer! – Andrew Gray Oct 22 '14 at 14:18
  • @Andrew Gray, ' I'm not sure where StackOverflow begins, and the Programmers stack properly begins' . I'll concur on that. – mccainz Oct 22 '14 at 14:19

1 Answers1

1

What you did is not a antipattern, but indeed best practice! See this style guide page for example: http://github.com/mgechev/angularjs-style-guide#services.

You did not circumvent the Angular way. The idea of the factory is the possibility to return constructor functions. The other alternative to creating services is the service-method of module which would automatically instantiate your service with the new keyword.

Take also a look at this other StackOverflow answer as reference: https://stackoverflow.com/a/20110304/669561

Community
  • 1
  • 1
DanEEStar
  • 6,140
  • 6
  • 37
  • 52