Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9296313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:48:17+00:00 2026-06-18T21:48:17+00:00

What would be the appropriate method for storing a global variable in Ember? For

  • 0

What would be the appropriate method for storing a global variable in Ember? For example, I have a User model in Ember, but I’d always like to know which specific instance of that model (including its id, name, email, etc) corresponds to the currently logged-in user.

Should I be storing this within Ember somehow? Or is it better to just attach a JS variable to the window object (e.g. window.currentUserId = 1;) and use that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:48:18+00:00Added an answer on June 18, 2026 at 9:48 pm

    Disclaimer: All the techniques shown below are my experience with EmberJS for the past few months, plus some discussions with my colleagues. Anything incorrect, please raise your voice. We are all in the learning phase (and ember.js doc sucks to the maxxx, so be kind 😉

    Currently there are two developers in my work place that are using Ember.js. We came to a conclusion that storing global variables in the global ApplicationController is much better than storing in the application namespace. This is because the retrieval of this value could be very messy if it’s stored in the application namespace. And this works well in closure too, and hence make the global namespace clean (and relatively hack free). You don’t want your user to do App.set to mess with the variable right?

    This is based on 1.0.0 pre-4.

    Considering you have this currentUserId global variable

    1. Storing in the namespace. jsFiddle demo

      (function() {
      
          function r() {
              return Math.round(Math.random()*999);
          }
      
          var MyApp = Ember.Application.create({
              currentUserId: null,
              ready: function() {
                  //demo purpose.
                  this.set('currentUserId', r());
              },
              rootElement: '#demo'
          });
          MyApp.ApplicationView = Ember.View.extend({
              templateName: 'application-view',
      
              //Direct child view
              innerView: Ember.View.extend({
                  templateName: 'inner-view',
                  setValue: function() {
                      this.set('controller.namespace.currentUserId', r());
                  }
              }),
      
              //Direct child view, but with a controller attached
              innerViewWithController: Ember.View.extend({
                  controller: Ember.Controller.create(),
                  templateName: 'inner-view-with-controller',
                  setValue: function() {
                      this.set('parentView.controller.namespace.currentUserId', r());
                  }
              }),
              getValue: function() {
                  alert(this.get('controller.namespace.currentUserId'));
              },
              setValue: function() {
                  this.set('controller.namespace.currentUserId', r());
              }
          });
      })();
      
    2. vs Storing in global ApplicationController jsFiddle demo

      (function() {
      
          function r() {
              return Math.round(Math.random()*999);
          }
      
          var MyApp = Ember.Application.create({
              ApplicationController: Ember.Controller.extend({
                  currentUserId: null,
                  init: function() {
                      //demo purpose
                      this.set('currentUserId', r());
                  }
              }),
              rootElement: '#demo'
          });
          MyApp.ApplicationView = Ember.View.extend({
              templateName: 'application-view',
      
              //Direct child view
              innerView: Ember.View.extend({
                  templateName: 'inner-view',
                  setValue: function() {
                      this.set('controller.currentUserId', r());
                  }
              }),
      
              //Direct child view, but with a controller attached
              innerViewWithController: Ember.View.extend({
                  controller: Ember.Controller.create(),
                  templateName: 'inner-view-with-controller',
                  setValue: function() {
                      this.set('parentView.controller.currentUserId', r());
                  }
              }),
              getValue: function() {
                  alert(this.get('controller.currentUserId'));
              },
              setValue: function() {
                  this.set('controller.currentUserId', r());
              }
          });
      })();
      

    Note that:

    1. If you chose to store in namespace, you will need to access it via the root controller all the time, so it’s actually same as storing in the applicationController with an extra namespace keyword.

    2. If you chose to store it in root applicationController, for whatever views that descend from the applicationView, you can easily access the variable in your templates with {{variableName}} without any dot traversals. By default Ember.Js look for variables via controllers.

    3. In the worst case, if your internal view need to have their own controller, accessing the global variable via root controller (or namespace) is slightly more painful because controllers are not linked, you will need to traverse up your views until you see the root controller. In Ember.JS, all views, by default, will have a controller set, and by default, parent’s controller. Which means if you never specify any controller, all descendant views are actually linked to root controller. To overcome this issue, you can do variable binding in the controller to easily solve the traversing ugliness.

    I do not suggest you to put such an important variable in the global window object as it will be easily modified by the user (and raises any potential issues). Putting it in the global namespace in the ember application namespace reduces the potential issue but not if you make it global

    My 2 cents 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have nested JSON strings that i would like to parse out the appropriate
would it be appropriate to use PHP's sscanf to read user submitted sentences and
Random question, sorry if not appropriate for stackoverflow, but I didn't think it would
I have a method that pulls some HTML via the HttpClient like so: public
I have recently read about how cursors should be avoided. Well, I would like
Objective-C, or Cocoa specifically, supports variadic arguments, like for example class the method on
I have a method that evaluates arithmetic expressions from a string, but it is
I would like to register a class (not an instance) when it's created... but
I have an application that uses several different Java classes and would like to
I have a base class which has a method for moving files to appropriate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.