如何在模板中访问全局变量? [英] How to get access to global variable in template?

查看:192
本文介绍了如何在模板中访问全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用REST API来获取设置来创建全局对象。我需要向REST API发出一个请求并获得设置,之后我想从任何控制器和任何模板访问这些设置。你能建议什么,那个问题最好的做法是什么?

I want to create global object with settings which I need to get from REST API. I need to make one request to REST API and get settings and after that I want to get access to these settings from any controllers and from any templates. What can you advice, what is the best practice for that problem?

推荐答案

概念



良好的做法是使用初始化的。它们允许将任何数据注入到路由,控制器或任何其他类型的对象中。

Concept

Good practice would be to use initializers. They allow injection of any data to routes, controllers or any other kind of object.

让我们来看一个例子,例如Ember的 .js官方网站

Lets take an example ( example from Ember.js official site )

1。 您有一个应用程序,你有一个 logger 这样的服务 -

1 . You have an Application and you have a logger service like this -

App = Ember.Application.extend();

App.Logger = Ember.Object.extend({
  log: function(m) {
    console.log(m);
  }
});

2。 现在你想要这个函数 log 在所有路线上可用像这样 -

2 . Now you want to have this function log to available on all routes like this -

App.IndexRoute = Ember.Route.extend({
  activate: function(){
    // The logger property is injected into all routes
    this.logger.log('Entered the index route!');
  }
});

3。 告诉ember注入名为 Logger 到所有路线。使用 initializer 像这样

3. Tell ember to inject an object named Logger to all routes. Use initializer Like this

//I want to inject something
Ember.Application.initializer({

  //this dependency name is logger
  name: 'logger',

  //whenever ember runs
  initialize: function(container, application) {

    //register my logger object under a name
    application.register('logger:main', App.Logger);

    //and use this service 'logger' in all 'routes'
    application.inject('route', 'logger', 'logger:main');
  }
});

通过这样,您可以在所有路由和控制器中提供应用级数据/代码。

With this you can have your application level data / code available in all routes and controller.

在控制器中获取数据后,您可以很容易地在模板中使用它。

Once you get your data in controller you can use it in templates pretty easily.

初始化程序可用于在其他一些服务已解决后运行。像我们这样的情况 store 存储是我们需要使用API​​调用服务器的对象(我们可以使用 $。getJSON()或任何其他没有问题)

Initializer can be used to run after some other services has been resolved. Like in our case store. store is the object we need to make API call to server in good way (We can use $.getJSON() or anything else no issues)

store 加载之后告诉初始化程序运行 / p>

Tell the initializers to run after store loaded

//I want to inject something but only after store resolved
Ember.Application.initializer({

  //this dependency name is logger
  name: 'logger',

  //wait for store object to be loaded, we need it to make API call
  after : 'store',


  //whenever ember runs
  initialize: function(container, application) {

      //grab the store object from container
      var store = container.lookup('store:main');

      //now you the store make that API call
      self.store.find('user',{current:true}).then(function(data){

            //we have the data we can inject it
            data = data.get('firstObject');
            container.lookup('controller:base').set('user', data);

            //user lookup success
            console.log("We have found an user. Yeah ember rocks.");

       });
   }

});

这篇关于如何在模板中访问全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆