javascript - Ember.js - How can I use a service in another service? -


i have setup 2 services shown in below initializer:

/* service initaializers */ var initaializer = {     name: 'services',     initialize: function(container, app) {         /* inject session service in routes , controllers */         app.inject('route', 'session', 'service:session');         app.inject('controller', 'session', 'service:session');         /* inject debug service in routes , controllers */         app.inject('route', 'debug', 'service:debug');         app.inject('controller', 'debug', 'service:debug');     } };  /* export */ export default initaializer; 

i can access both session service , debug session routes/controllers user this.session , this.debug.

what having trouble doing accessing of functions in debug service session service.

app/services/debug.js

/* debug service */ var service = ember.object.extend({     /* start debug */     init: function() {         console.log('debug started!'); // appear in console.       },       logsomething: function(i) {         console.log(i);  // work routes/controllers.      } });  /* export */ export default service; 

app/services/sessions.js

/* session service */ var service = ember.object.extend({     /* start session */     init: function() {           console.log('session started!'); // appear in console.         this.debug.logsomething('test'); // gives error.     },     sayhi: function() {         console.log('hello session service'); // work routes/controllers.     } });  /* export */ export default service; 

the line gives console error this.debug.logsomething('test');.

the error is: uncaught typeerror: cannot read property 'logsomething' of undefined

what need in order access function in service service?

you have injected these objects in route , controllers. need inject in each other if want them accessible

ok, believe possible. need inject debug object in session one.

you can so:

first register factories:

app.register('utils:debug', app.debug); app.register('service:session', app.session); 

and inject debug in session:

app.inject('service:session', 'debug', 'utils:debug'); 

or can inject debug in services:

app.inject('service', 'debug', 'utils:debug'); 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -