lundi 5 novembre 2018

How to add a property to the App object in an EmberJS application

I am trying to add some timeout functionality to my application and using an old article for reference (available here).

I'm falling at the first hurdle in that I don't understand how to add a lastActiveTime property to my App object. The ember documentation states

An instance of Application is the starting point for every Ember application. It helps to instantiate, initialize and coordinate the many objects that make up your app.

Each Ember app has one and only one Application object. In fact, the very first thing you should do in your application is create the instance...

This is obviously being handled in my working application and I can view the App object but I can't see where to add properties to it. I tire dot add it in app.js:

import Application from '@ember/application';
import Resolver from './resolver';
import loadInitializers from 'ember-load-initializers';
import config from './config/environment';

const App = Application.extend({
  modulePrefix: config.modulePrefix,
  podModulePrefix: config.podModulePrefix,
  lastActiveTime: null,
  ready: function () {
    App.lastActiveTime = new Date();
  },
  Resolver,

});

loadInitializers(App, config.modulePrefix);

export default App;

or in the environment config:

module.exports = function (environment) {
  let ENV = {
    modulePrefix: 'insight',
    environment,
    rootURL: '/',
    locationType: 'hash',
    EmberENV: {
      FEATURES: {
        // Here you can enable experimental features on an ember canary build
        // e.g. 'with-controller': true
      },
      EXTEND_PROTOTYPES: {
        // Prevent Ember Data from overriding Date.parse.
        Date: false
      }
    },

    //turn on/off config
    export_data: false,

    APP: {
      // Here you can pass flags/options to your application instance
      // when it is created
      lastActiveTime: null,
       ready: function () {
         App.lastActiveTime = new Date();
       },


    }
  };

and neither produce an App.lastActiveTime property. I'm actually unable to import App in the environment config so that can't be correct.

I don't know if I'm failing in Ember or failing some basic aspect of Object handling. Any advice is appreciated




Aucun commentaire:

Enregistrer un commentaire