PDA

View Full Version : Dynamically adding fields to Ext.DataStore?



rbraddy
23 Aug 2009, 5:50 AM
Is it possible to dynamically define the public "fields" for a DataStore?

I have a JsonStore object that I want to reuse with different columns/fields:




The JsonStore contains a JsonWriter:

this.writer = new Ext.data.JsonWriter({
encode: false,
returnJson: false,
listful: false,
writeAllFields: false
});

The following code uses an instance of the JsonStore called "answerStore":

var answerStore = theStores.answerStore; // get the answer store
// set the answerStore "schema" dynamically
answerStore.fields = [
{name:'name', type:'string'}, // project name required for lookup
{name:'launchdate', type:'string'},
{name:'launchtimeofday', type:'string'},
{name:'launchzone', type:'string'},
{name:'launchtime', type:'string'}
];

var rec = new answerStore.recordType({
name : projname,
launchdate: launchdate,
launchtimeofday: launchtime,
launchzone: launchzone,
launchtime: this.launchTime().getTime()
});

answerStore.createRecord(rec, this, this.onFinishSuccess, 'updateanswers');


The JSON sent to the server by the JsonWriter object is:

{"answers":{"name":"My Project 1","id":"ext-record-61"},"xaction":"create"}

The "name" field is correctly set, but the others are missing from the JSON string sent to the server by the JsonWriter (e.g., launchdate, launchtimeofday, etc. fields).

Are these "fields" definitions even required for the JsonWriter.create() method to work? (or does it simply use the record's fields to form the JSON output)?

The JsonStore seems to work just fine if I statically define "fields" at construction time... (the API docs says "fields" is readonly, so perhaps there's no way to dynamically change these fields for a store)

Anyway, it's unclear what I'm doing wrong here that's preventing all my data fields to be sent.

Thanks in advance for an assist.

Rick

rbraddy
24 Aug 2009, 1:50 PM
Hmmm. Perhaps not then

rbraddy
24 Aug 2009, 1:58 PM
I found a workaround, but it's not ideal...

One can predefine all the "fields" at the time the DataStore is constructed, then use them incrementally by specifying only the fields of interest in a given RecordType and createRecord() call.

Would be nice to be able to override the "field" definitions to allow for data store reuse.

hendricd
24 Aug 2009, 2:53 PM
@rbraddy -- For successful field hacks, the store's reader should be your target.

store.reader.meta.fields specifically.

When you define a writer on the store, it gets a reference to the reader.meta you defined (thus it's field metaData). So, only fields currently known to the reader instance (when the store was instantiated) will be handled by the writer.

You would need to simulate the various onMetaChange actions to get it all to work seemlessly after-the-fact.

rbraddy
24 Aug 2009, 4:27 PM
Thanks for pointing me in the right direction, Doug.

Rick

BlueCamel
31 Aug 2009, 11:54 AM
Were you able to find a solution to this? I'm in the same situation. I like you're hack of adding all possible fields to the store when the store is defined. However, for my app I plan to let the user select from over 16K fields dynamically. This is likely a few too many to add to store on create. :)

jnicora
31 Aug 2009, 1:45 PM
Can you construct a new column model then call the reconfigure method? I think that will do all the fields work for you. But maybe you aren't using this store with a grid.



grid.reconfigure(store, colModel);