PDA

View Full Version : Get STORE INSTANCE from another Controller Action



arnoldvillasanta
16 Aug 2014, 5:15 AM
Hi,

I have a FORM which is loaded with companyRecord from companyStore of ControlAction1.
I have ControlAction2 which sets the companyRecord with newData via form.getValues().
Now I want to execute companyStore.sync() from ControlAction2 (persist new Data to Database).
How can I do that?
Is the companyStore instance of ControlAction1 reachable from controlAction2?

chamacs
16 Aug 2014, 5:57 AM
If your app only uses one instance of a store you can access it from any controller with:


Ext.getStore('storeName');
or within a controller:

this.getStore('storeName');

Declaring the same store name in the controller's 'stores' array doesn't create multiple instances.
Hope that helps.

arnoldvillasanta
16 Aug 2014, 6:54 AM
To be more precise:

I have storeName in Store Manager, then:

(Inside controllerAction1)
var storeName1 = this.getStore('storeName');
storeName1.load({//callback function: get record from storeName1 then load record to Form1});

(Inside controller Action2)
var record = Form1.getRecord();
record.set(Form1.getValues());
Sync store >>>>>> this.getStore('????').sync

Does storeName behaves like a static class in java?
What if I have var storeName2 = this.getStore('storeName')?

chamacs
16 Aug 2014, 7:22 AM
To be more precise:

I have storeName in Store Manager, then:

(Inside controllerAction1)
var storeName1 = this.getStore('storeName');
storeName1.load({//callback function: get record from storeName1 then load record to Form1});

(Inside controller Action2)
var record = Form1.getRecord();
record.set(Form1.getValues());
Sync store >>>>>> this.getStore('????').sync

Does storeName behaves like a static class in java?
What if I have var storeName2 = this.getStore('storeName')?

storeName1 and storeName2 reference the exact same store instance. 'storeName' is a 'key' reference to a store instance in the store manager. Like a HashMap in Java.

chamacs
16 Aug 2014, 7:35 AM
If you declare your store in the stores array in your controller:


stores : ['StoreName']

When the controller is initialized Ext will create and save an instance of the 'StoreName' if it doesn't already exist.
Declaring the same 'StoreName' in different controllers does not create multiple instances. So every call to this.getStore('StoreName') will return the same instance, regardless of controller instance..

arnoldvillasanta
16 Aug 2014, 11:19 PM
Hi Chamacs,


My usecase is that:
1. Action1 will contain the creation of storeName1 and have its record loaded to Form1.
2. Action2 will contain the manipulation of Form1's field values (edit) and sync the changes... which I think will require storeName1 instance....


In my case, the storeName1 in Action1 has storeName1.getCount() = 1.

In Action2, I console.log and got storeName2.getCount() = 0.

It could have been storeName2.getCount() = 1 if you said both instances are referring to storeName...

I could simply do storeName2.add(Form1.getRecord()) and call a function to call the UPDATE api... but I like the elegance of simply calling SYNC... and be consistent in all my DB persistence codes.

So my question is:
Is there a way for controlAction2 to call storeName1 located in controlAction1?

chamacs
17 Aug 2014, 6:40 AM
A couple of ideas:

1.
This might be what you're looking for:
http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.AbstractStore-cfg-storeId

Based on your use case, I assume you could do something like this in controller 1:

var store = Ext.create('App.store.MyStore', {
storeId : 'controller1Store'
});
// use store....

In controller 2, to get access to the store you should be able to do this:

var store = this.getStore('controller1Store');

2.
(more of a hack)
in controller1:

var store = Ext.create('App.store.MyStore');
// use store....
this.storeReference = store;

in controller 2:

var store = this.getController('Controller1').storeReference;

Hope this gives you some ideas.

arnoldvillasanta
17 Aug 2014, 6:10 PM
Hi Chamacs,

Thanks for the help.
I will look into these things as you've suggest.

I tried the first one and had a Synchronization warning message and am looking at it right now.

Thanks for the tips.