samedi 14 mars 2020

How to import global variable in Ember?

I am new to Ember, and am trying to use a global variable in the config / environment file to then import it into several adapters and models. I need this to change the variable in one place, instead of editing each file. In this case, the global variable is a string with the address of the api server. The variable is named apiRoot. I tried to use the following configuration, but it does not work. Please tell me what needs to be done, if this is possible in the Ember, or maybe there is another way? Thanks for any help!

Environment File:

'use strict';

module.exports = function(environment) {
  let ENV = {
    modulePrefix: 'front',
    environment,
    rootURL: '/',
    locationType: 'auto',
    EmberENV: {
      FEATURES: {
      },
      EXTEND_PROTOTYPES: {
        Date: false
      }
    },

    APP: {
    }
  };

  if (environment === 'development') {
  }

  if (environment === 'test') {
    ENV.locationType = 'none';
    ENV.APP.LOG_ACTIVE_GENERATION = false;
    ENV.APP.LOG_VIEW_LOOKUPS = false;

    ENV.APP.rootElement = '#ember-testing';
    ENV.APP.autoboot = false;
  }

  if (environment === 'production') {
  }

  ENV.apiRoot = 'http://localhost:5555/api';

  return ENV;
};

Adapter:

import RESTAdapter from '@ember-data/adapter/rest';
import ENV from '../../config/environment';

export default RESTAdapter.extend({
  host: ENV.apiRoot,
  pathForType() {
    return "posts";
  }
});



Aucun commentaire:

Enregistrer un commentaire