Is this.app still available for quasi-global variables?

Under Enyo 2.7, does every kind still have an "app" property? Is there another place to put application-global variables?

Comments

  • Every Component should still have the app property in 2.7. There aren't any new features specifically for sharing state in 2.7. You could use a module exporting an regular object or an enyo Component as well.
  • Does the require() used in Enyo 2.7 guarantee that it always returns the same object, when called from different files?

    Neither require.js nor Node guarantee a singleton, IIRC.
  • You should always get the same object with enyo's require()
  • I have two modules I want in available anywhere in my app. One is the current logged in user and the other is a task timer. I can't grok how to add those to the Application - should I require() them in the App.js generated by my onyx-webos-app template (basically the same as onyx-app template)?
    var
    	kind = require('enyo/kind'),
    	Application = require('enyo/Application'),
    	MainView = require('./views');
    
    module.exports = kind({
    	kind: Application,
    	view: MainView
    });
    Would I add them in a components block to Application?
    var
    	kind = require('enyo/kind'),
    	Application = require('enyo/Application'),
    	myUser = require('./data/myUser'),
    	myTimer = require('./data/myTimer'),
    	MainView = require('./views');
    
    module.exports = kind({
    	kind: Application,
    		components: [{kind: myUser}, {kind: myTimer}],
    	view: MainView
    });
    Something like that?

    And then is the access from any component like this?
    this.app.$.myUser
    -Thanks in advance!
  • Assuming that myUser and myTimer were enyo kinds, that should work. If they are singletons or something else (e.g. POJO), you could require() them directly wherever you needed them. For application state, I would probably lean to attaching them to the app in some way. For utilities, I'd prefer directly requiring them where they are used.

    HTH!
  • this.app comes undefined in enyo 2.7. How do we set it ?
Sign In or Register to comment.