...just couldn't let go, and ended up with this, which is working but it
feels like one big hack.
There's got to be a more elegant solution. If anyone can chime in..?
angular.module('app')
.provider('LoggerFactory', function() {
this.$get = function(self) {
var module = ''
self.setModule = function(module_) { module = module_ }
self.$get = function() {
return {
info: function(message) {
console.info(module + '::' + message)
}
}
}
}
})
.provider('RootLogger', function(LoggerFactoryProvider) {
LoggerFactoryProvider.$get(this)
})
.config(function(RootLoggerProvider) {
RootLoggerProvider.setModule('root')
})
.provider('DashboardLogger', function(LoggerFactoryProvider) {
LoggerFactoryProvider.$get(this)
})
.config(function(DashboardLoggerProvider) {
DashboardLoggerProvider.setModule('dashboard')
})
On Thu, Mar 6, 2014 at 12:08 PM, Antoine van Wel
<[email protected]>wrote:
> Hi,
>
> I'm trying to wrap my head around what would be a clean solution to have
> multiple instances of a service.
>
> As my first attempt;
>
>
> function LoggerFactory(module) {
> return {
> info: function(message) {
> console.info(module + '::' + message)
> }
> }
> }
> angular.module('app')
> .value('RootLogger', LoggerFactory('root'))
> .value('DashboardLogger', LoggerFactory('dashboard'))
>
>
> Which works fine but having that LoggerFactory as a function is definitely
> a no-go..
>
>
>
> So my second attempt, wrap the LoggerFactory in a service
>
> angular.module('app')
> .service('LoggerFactory', function() {
> this.build = function(module) {
> return {
> info: function(message) {
> console.info( module + '--' + message )
> }
> }
> }
> })
> .factory('RootLogger', function(LoggerFactory) {
> return LoggerFactory.build('root')
> })
> .factory('DashboardLogger', function(LoggerFactory) {
> return LoggerFactory.build('dashboard')
> })
>
> Which is a bit more lengthy since we cannot use the LoggerFactory without
> dependency injection.
>
> Now this is working fine but I can imagine having a more complicated
> service with more complicated "build" behavior, which would become more..
> complicated. Anyway, what about using .provider here, and configure
> instances using .config, after all that's what providers are meant for..
> right? So my third attempt, with providers..
>
>
> function LoggerProvider() {
> var module = ''
> this.setModule = function(module_) { module = module_ }
>
> this.$get = function() {
> return {
> info: function(message) {
> console.info(module + '::' + message)
> }
> }
>
> }
> }
>
> angular.module('app')
> .provider('RootLogger', LoggerProvider)
> .config(function(RootLoggerProvider) {
> RootLoggerProvider.setModule('root')
> })
> .provider('DashboardLogger', LoggerProvider)
> .config(function(DashboardLoggerProvider) {
> DashboardLoggerProvider.setModule('dashboard')
> })
>
> Ok, working fine, looking good, except for that LoggerProvider being a
> function again. So, fourth time's a charm:
>
>
> angular.module('app')
> .service('LoggerProviderFactory', function() {
> this.build = function(self) {
> var module = ''
> self.setModule = function(module_) { module = module_ }
> self.$get = function() {
> return {
> info: function(message) {
> console.info(module + '::' + message)
> }
> }
> }
> }
> })
> .provider('RootLogger', function(LoggerProviderFactory) {
> LoggerProviderFactory.build(this)
> })
> .config(function(RootLoggerProvider) {
> RootLoggerProvider.setModule('root')
> })
> .provider('DashboardLogger', function(LoggerProviderFactory) {
> LoggerProviderFactory.build(this)
> })
> .config(function(DashboardLoggerProvider) {
> DashboardLoggerProvider.setModule('dashboard')
> })
>
>
> Alright setting aside this is not exactly pretty code, more importantly,
> this is not working at all, it fails with an error message
>
> [$injector:modulerr] Failed to instantiate module app due to:
> Error: [$injector:unpr] Unknown provider: LoggerProviderFactory
>
>
>
> This is where I hope one of you people can step in and tell I'm doing this
> all wrong :-)
>
>
>
> Antoine
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.