2

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);
  },
});  

1 Answer 1

2

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);
2
  • Thank you! Your answer is right on. Question if I may: is App.register('session:main', App.Session.create(), { instantiate: false, singleton: true }); equivalent to App.register('session:main', App.Session, { singleton: true }); ? Oct 28, 2014 at 2:07
  • Indeed it is, it's even the same as App.register('session:main', App.Session); since singleton:true is the default ;) But I never shy away from explicitly defining things when learning them.
    – Kingpin2k
    Oct 28, 2014 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.