Creating a global class


All, I’m new to ember and more familer with Angular/Angular2 than I am wit this framework. So far I like it but I can’t seem to wrap my head around some of the terminology and design paradigms.

I’m working on a project where I need to create a global class with persistence and can’t quite figure out what the correct way to do this is.

Take the following plain javascript for example

// Pseudo code, untested, off the hip
// UselessClass
var UselessClass = {
  name: 'nullnillzero',
  setName: function(name) { = name;
  getName: function() {
  sayHello: function(name) {
    if(name === undefined)
      console.log('Hello ' +;
      console.log('Hello ' + name);

Then I could do something like this

// Pseudo code, untested, off the hip
var myUselessClass = new UselessClass();
myUselessClass.sayHello('nullnillzero');  // Results in: console.log('Hello nullnillzero');
myUselessClass.setName('null'); // Results in settings null as name
console.log(myUselessClass.getName()); // Results in console.log('null');
myUselessClass.sayHello(); // Results in console.log('Hello null');

How would I create a global class as shown in the first code block to be used globally in ember?



If you have just started with Ember, it is quite useful to go through on a few tutorials:

In Ember World, it is prefered to use provided building tool (ember-cli), under the hood, they use babel to transpile your code to native javascript, so you can use the ES6 syntax to create javascript class, but probably you would like to extend the default Ember.Object:

Using globals is not really suggested, however, you can use it. For example emberjs jsbin still uses this old way to experiment with ember features. Check out the default example:

If you need something, what you can use everywhere in your application, you can create an Ember.Service, you can inject this service where you would like to use it:

Hope these links can help. :slight_smile: