简体   繁体   English

如何始终使用Ember.JS加载余烬引擎

[英]How to always load an ember-engines with Ember.JS

I am currently trying to access my ember-engines wherever I am. 我目前正在尝试访问我在任何地方的余烬引擎 I want to have the name of each engines, because I want to display every engines in my main application. 我想要每个引擎的名称,因为我想在主应用程序中显示每个引擎。
The problem here is that I can't find a way to catch there name, because the engines are loaded only if we navigate to a page related to the engines. 这里的问题是,我找不到找到名称的方法,因为只有在导航到与引擎相关的页面时,才会加载引擎。
This is the services that is normally catching every name of each engines, call it engines-manager : 这是通常捕获每个引擎的每个名称的服务,称为engine-manager

import Ember from 'ember';
import App from '../app';

/**
 * This service will help us manage every engines that are in our application,
 */

export default Ember.Service.extend({
    availableEngines: [],

    addEngines(engines)
    {
        this.get('availableEngines').push(engines);
        console.log(this.get('availableEngines'));
    }
});

And then in every engine, more precisely, in every anEngines/addon/engine.js of every engine, I put : 然后,在每个引擎中,更确切地说,在每个引擎的每个anEngines / addon / engine.js中,我都输入:

import Engine from 'ember-engines/engine';
import loadInitializers from 'ember-load-initializers';
import Resolver from './resolver';
import config from './config/environment';
import Ember from 'ember';

const { modulePrefix } = config;

const Eng = Engine.extend({
    modulePrefix,
    Resolver,
    dependencies: {
        services: [
            'engines-manager'
        ]
    },
    enginesManager: Ember.inject.service('engines-manager'),
    init() {
        this._super(...arguments);
        this.get('enginesManager').addEngines('nameOfMyEngines');
    }
});

loadInitializers(Eng, modulePrefix);

export default Eng;

This method is working if we go on the engines related page, but I dont want to go to every page to load them, so here is my question: how to always load an ember-engines? 如果我们转到与引擎相关的页面,则此方法有效,但我不想转到每个页面来加载它们,所以这是我的问题: 如何始终加载余烬引擎?

PS: setting my lazy-loading to true or false doesn't change anything. PS:将我的lazy-loading设置为truefalse不会更改任何内容。

Thanks to the slack community, we found a workaround. 多亏了闲散的社区,我们找到了解决方法。 The engine are not means to load if we don't enter it. 如果我们不输入引擎,则意味着无法加载引擎。
So here is a workaround that don't really respond to the answer, but at least you can access to the data present in the engine, like the name, or the services. 因此,这是一种无法真正回答问题的解决方法,但是至少您可以访问引擎中存在的数据,例如名称或服务。

First 第一

You have to create a container in a services, in your app: 您必须在应用程序的服务中创建一个容器:

export default Ember.Service.extend({
 fetchEngines()
    {
    let container = Ember.getOwner(this).lookup('application:main').engines; 
    }
});

That's it, if you know how to use this data, you have the capacity to fetch the name of the engines for example. 就是这样,如果您知道如何使用此数据,则可以获取例如引擎的名称。

Secondly 其次

You have now a container, but the data cannot be use like this, container here is not a JSON object for example. 您现在有了一个容器,但是不能像这样使用数据,例如,这里的容器不是JSON对象。

let newObject = [];
for (let key in container) {
     if (container.hasOwnProperty(key))
         newObject.push(key);
}

Here newObject is an array with the name of every engines that are present in your app.js in it. 这里的newObject是一个数组,其中包含app.js中存在的每个引擎的名称。 Hope that help some people. 希望对一些人有所帮助。 I will not accept this response, because this is not what the question ask, but this is a beginning. 我不会接受这个答复,因为这不是问题要问的,但这是一个开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM