greenTree
greenTree

Reputation: 305

Multiple classes in Ext JS to update and share the same config settings

I am fairly new to using Ext JS 4.0.7, and have recently discovered the Config options, so I have set up the following config class, which initialises with the following:

Ext.define('CurrentRowSelection', {
extend: 'Ext.app.Controller',

config: {
currentAccountID: {
  $value: 'Empty',
  cached: true,
  evented: true
 }
},

initialize: function(config) {
this.callParent(config);
}

});

I am able to update this config quite nicely from another class's listener event, which is right clicking on a row record and grabbing the currentAccountID value. However, every time I try to access the new, updated, config from a new class after initialising - it keeps reverting back to the 'Empty' value.

The following is used to set the config from the class containing the listener:

var CurrentRowSelection = new CurrentRowSelection();
CurrentRowSelection.setCurrentAccountID(1);

However, when I create a new constructor in a new class, with the hope of accessing this newly updated config from elseware, my console.log is telling me that it is continuously reverting back to the default values, as it seems to be initialising each time a new constructor is created - which makes sense, but I need to stop it.

How can I do thisand make sure that this value is saved all the time, every time? Thank you.

Upvotes: 0

Views: 209

Answers (1)

Alexander
Alexander

Reputation: 20224

Don't make new controllers every time. Every controller should be instantiated only once, because if you instantiate multiple controllers of the same type, every event binding you prepare in the control method is executed multiple times (the control method usually is the most important method of a controller, although there may be exceptions.)

So every controller should exist as a single instance only, and that singleton would be accessed using getController wherever and whenever you need it:

MyAppName.app.getController("ControllerName").setCurrentAccountID(1)

That said, I would recommend to look into using stores for data storage, not controllers. Stores offer the possibility to synchronize the settings with a server (create, read, update, delete). If you want to create a singleton store, you would give it a storeId and use Ext.getStore() to access the store.

For example, the config settings in my application are in the first record of the store with storeid AllSettings:

Ext.getStore("AllSettings").getAt(0).get("groupDiscoveryCacheHours")

Upvotes: 1

Related Questions