View Full Version : Quick question about Store and data model

18 May 2012, 7:14 AM

I have a question that is it possible to reset data model of a store in the run time. For example depending upon selection of the user the store can switch from a data model to another one.
Alternatively, can we reset fields in a data model in the run time?

I have looked into Store, DataModel, Grid Panel API but I don't find any functions that allow me to do those things. You would have some ways.

Your answers or hints would help to solve my puzzle.

Thanks and regards,

18 May 2012, 8:00 AM
By "reset fields in a data model at run time" do you mean reset them to default values, nulls, or previous values?

18 May 2012, 8:32 AM
Hi castitas,

I meant to change the fields. For example:

['id', 'firstname', 'lastname'] to ['id', 'projectname', 'dateline']

I tried to get the model then do like this: model.fields = [new fields come here]

but somehow although data is loaded from the backend correctly but data is not displayed in the new field in the form except id (both field set have id field).


18 May 2012, 9:38 AM
I don't know about changing the model of an existing store, but you could certainly create a new model/store based on your needs.

18 May 2012, 9:56 AM
Are you looking at completely different models, or is there some overlap?

18 May 2012, 12:05 PM
Hi Castitas,

I need to replace the model's fields array with a new fields array.

Can you show me both ways, replace only fields array and replace with a complete new model?


18 May 2012, 1:56 PM
Probably not. I looked into it a little and it's not going to be easy. The model was not designed to switch fields, and replacing the fields requires more than model.fields = [...]. Fields contain events, listeners, sorters, etc. Even if you get it right, other variables in the model could be related to the fields the model was instantiated with. So the short answer appears to be no.

But there should be a way to satisfy your need inside the framework.

If you only need one model (such as for a form), a better solution would be to replace the form's model every time you need to change. This way, you don't even need a store.

Why do you want to change fields? What's the problem?

18 May 2012, 3:41 PM

Why do you want to change fields? What's the problem?

My situation is I have a grid panel that show different types of data, which has different columns. A form panel next to this grid panel displays data in a form so that the user can do administrative tasks. So what I want is when the user selects a type of data in the grid panel, I update the form and the model according to the selected data type. After the store loads data from the backend I populate data by loading this data model into the form. By doing it the form is dynamically changed and behind the scene data model is also updated with the interaction of the user.

If changing model's fields is not feasible then is there any way to replace a store with a new one dynamically? Or any way to replace a model of a store?

Any suggestions is very appreciated.


20 May 2012, 11:40 PM
if you have a store which fields has to be updated, you can use the metadata functionality. You have to send those fields in a metadata.fields construct from the server. You can have a look at this in:

t (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.reader.Json)he (somehow unformatted) Response Metadata abstract at the beginning should help you.

21 May 2012, 6:47 AM
Hi born,

Thank you very much! Your reference is really helpful to me.


21 May 2012, 7:11 AM
Did that fix your problem?

21 May 2012, 8:21 AM
I have not tried the way suggested by Born, but will soon. Yesterday, I changed my app making the form to load data from the backend every time when the user selects an entry in the list. In this way I have to use the function load and submit and the only thing I don't like this approach is I can't send form data in a JSON object to the back end.


23 May 2012, 8:47 PM

After upgrading to 4.1 I tried the solution suggested by Born and the problem was fixed. Now my app is more dynamic and the grid and form work smoothly.

Thank you very much Born and Castitas!


24 May 2012, 7:13 AM
Great! Make sure you mark his answer. Happy coding.