Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a global variable from any app controller

Tags:

I need to use a global variable (user context, available from all the code) I have read some posts regarding this topic but I do not have a clear answer.

App = Ember.Application.create({   LOG_TRANSITIONS: true,   currentUser: null }); 
  1. Is it a good practice to set the currentUser global variable in the App object ?
  2. How to update and access the currentUser property from all the controllers used in the app ?
like image 697
fvisticot Avatar asked Jun 02 '13 00:06

fvisticot


People also ask

Can global variable be accessed anywhere in program?

Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program. A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration.

How do you access global variables?

Global Variable: The variable that exists outside of all functions. It is the variable that is visible from all other scopes. We can access global variable if there is a local variable with same name in C and C++ through Extern and Scope resolution operator respectively.

What is the best way to declare and access a global variable?

The clean, reliable way to declare and define global variables is to use a header file to contain an extern declaration of the variable. The header is included by the one source file that defines the variable and by all the source files that reference the variable.

How do you call a global variable in Java?

To define a Global variable in java, the keyword static is used. Java actually doesn't have the concept of Global variable, it is known as class variable ( static field ). These are the variables that can be used by the entire class.


1 Answers

Is it a good practice to set the currentUser global variable in the App object ?

No, this is not a good practice. You should avoid using global variables. The framework does a lot to make this possible - if you find yourself thinking a global variable is the best solution it's a sign that something should be refactored. In most cases the right place is in a controller. For example, currentUser could be:

//a property of application controller App.ApplicationController = Ember.Controller.extend({   currentUser: null });  //or it's own controller App.CurrentUserController = Ember.ObjectController.extend({}); 

How to update and access the currentUser property from all the controllers used in the app ?

Use the needs property. Let's say you've declared currentUser as a property of ApplicationController. It can be accessed from PostsController like this:

App.PostsController = Ember.ArrayController.extend{(   needs: ['application'],   currentUser: Ember.computed.alias('controllers.application.currentUser'),   addPost: function() {     console.log('Adding a post for ', this.get('currentUser.name'));   } )} 

If you need to access currentUser from a view/template, just use needs to make it accessible via the local controller. And if you need it from a route, use the route's controllerFor method.

like image 185
Mike Grassotti Avatar answered Oct 04 '22 11:10

Mike Grassotti