Reputation: 184
I've got a problem that has driven me round in circles, and I'd like to know the correct solution. It's likely that something fundamental and obvious hasn't clicked in my head, but as I'm knew to Ember.js I'd appreciate your time.
I'd like to use a global variable in ember.js to store a users id and name.
At the moment, I've done this by adding variables to application controller:
App.ApplicationController = Ember.Controller.extend({
//user name initially set to Tom
name:'Tom',
//using local storage - only works in modern browsers
userID: localStorage['userID']
});
Question 1: In the application template, I can display the "name" variable by using {{name}}. Fine. I also have a 'login' template, how do I access the "name" variable from the login template? I've tried using {{App.name}} but this doesn't work? Do I have to pass the application controller variables to the login controller and if so, do I do this from the login router?
Question 2: To set the userID from the login controller, is it a case of using:
this.controllerFor('application').set('userID','Fred');
or should I be using:
Ember.set('App.userID','Fred');
OR, should I be creating a separate user controller or perhaps user object to store this data?
Upvotes: 3
Views: 1328
Reputation: 47367
You can use the needs property to tell ember that you need access to another controller, then you can create an alias property (essentially creating a reference to another property).
App.LoginController = Ember.Controller.extend({
needs: ['application'],
userId: Ember.computed.alias('controllers.applications.userId')
});
Then from the login controller you can do this.set('userId', 'fred');
Upvotes: 2