Samuel Segal
Samuel Segal

Reputation: 423

Ember.js: Cannot access injected object

I created a Session object:

App.Session = Ember.Object.extend({
  user: null,
  userID: '',
});

I injected this object:

Ember.Application.initializer({
name: 'login',
initialize: function (container, application) {
  App.register('session:main', App.Session, { instantiate: false, singleton: true }); 
  // Add `session` object to route to check user
  App.inject('route', 'session', 'session:main');
  // Add `session` object to controller to visualize in templates
  App.inject('controller', 'session', 'session:main');

  App.register('firebase:main', App.Firebase);
  // Add `session` object to route to check user
  App.inject('route', 'firebase', 'firebase:main');
  // Add `session` object to controller to visualize in templates
  App.inject('controller', 'firebase', 'firebase:main');
  }
});

I want to set the userID property of Session to the ID of the current user as per follow:

Ember.Route.reopen({
  beforeModel: function (transition) {
    var isAuth = false;
    var user = '';
    var store = this.store;
    var _this = this;
    var firebase = new Firebase("https://dynamicslife.firebaseio.com");
    firebase.onAuth(function(authData) {
      if (authData) {
        isAuth = true;
        _this.session.userID = authData.id; //THIS OPERATIONS IS NOT WORKING!!!!!!
      } else {
        isAuth = false;
      }
    });

    // These routes you do not need to be logged in to access.
    var openRoutes = ['home','index','about','stories'];
    var testMode = false;
    if (testMode == false) {
    if (openRoutes.indexOf(transition.targetName) === -1 && isAuth === false) {
      console.log('Please login to access this information');
      this.transitionTo('stories');
    }  
  }
 }
});

In the code expression identified above by: //THIS OPERATIONS IS NOT WORKING!!!!!!, the authData.id contains the proper ID value. However the _this.session.userID remains undefined.

I tried to replace _this.session.userID = authData.id; with _this.session.set('userID', authData.id); but this gave me the same error.

I think that maybe the Session object is not injected into Ember.Route.reopen. If this is the case, how can I get access to the Session object from the the Ember.Route.reopen?

Because the session.userID is undefined, I get the following error:

Error while processing route: statistics.index session is not defined ReferenceError: session is not defined

When Ember run this:

App.StatisticsRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('user', session.userID);
  },
});  

Upvotes: 2

Views: 350

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

You're telling it not to instantiate App.Session, which means it's the object you want to use, but it's a class not an instance

App.register('session:main', App.Session.create(), { instantiate: false, singleton: true }); 

and session exists on this in the route, not on the global namespace.

App.StatisticsRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('user', this.session.get('userID'));
  },
});  

And you should definitely use a setter

 _this.session.set('userID', authData.id);

Upvotes: 2

Related Questions