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

前端 未结 2 1694
轻奢々
轻奢々 2020-12-16 19:36

Using this example: jsbin

Where would I put this line of code:

App.name = \'White\'

in the EAK file structure so that it will rende

相关标签:
2条回答
  • 2020-12-16 20:13

    Putting this in initializers is great but if your app is not using much global data or just need a flag, can use it directly :

    window.App = Ember.Application.create(
      ready: ->
        this.register('current:view', window.current_view, {instantiate: false});
        this.inject('controller', 'isCurrentView', 'current:view');
      rootElement: '#ember-app'
    )
    

    Now you access {{isCurrentView}} in all templates

    0 讨论(0)
  • 2020-12-16 20:28

    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}}
    
    0 讨论(0)
提交回复
热议问题