Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do you place a simple variable in the Ember App Kit file structure so it can be accessed by a template file?

Tags:

ember.js

Using this example: jsbin

Where would I put this line of code:

App.name = 'White'

in the EAK file structure so that it will render on the index page as in the example?

like image 355
EdgarAllenPoe Avatar asked May 18 '14 03:05

EdgarAllenPoe


People also ask

How do I use Ember components?

Ember components are used to turn markup text and styles into reusable content. Components consist of two parts: a JavaScript component file that defines behavior, and its accompanying Handlebars template that defines the markup for the component's UI. Components must have at least one dash in their name.

How do you create an ember object?

To define a new Ember class, call the extend() method on EmberObject : import EmberObject from '@ember/object'; const Person = EmberObject. extend({ say(thing) { alert(thing); } }); This defines a new Person class with a say() method.


1 Answers

I'm not sure if this is the right way. But generally EAK tries to avoid polluting the global namespace. So a method to have globals accessible every where is to use initializers to register a globals dependency and inject them to all the controllers. This is the same way ember data injects the store to the controller.

Inside app/initializers create global.js file

var globals = Ember.Object.extend({
  name: 'Edgar Allen Poe'
});

export default {
  name: "Globals",

  initialize: function(container, application) {
    container.typeInjection('component', 'store', 'store:main');
    application.register('global:variables', globals, {singleton: true});
    application.inject('controller', 'globals', 'global:variables');
  }
};

This will inject the globals to all the controllers. You can refer it in a template like

{{globals.name}}
like image 112
blessanm86 Avatar answered Sep 19 '22 22:09

blessanm86