PDA

View Full Version : Create a new Ext.data.Field instance with string type, but meet some issue



evilight
10 Jan 2012, 2:10 AM
I define a field and want to add a new filed for it


var myFields =
[
{name: 'testname', mapping: 'testname', type: 'string'},
{name: 'passrate', mapping: 'passrate', type: 'float'},
{name: 'date1', mapping: 'date1', type: 'string'},
];

I tried solution1:


var newfield = Ext.create('Ext.data.Field', { name: 'date2', mapping: 'date2', type: 'string' } );

solution2:


var newfield = Ext.create('Ext.data.Field', { name: 'date2', mapping: 'date2', type: Ext.data.Types.STRING } );



and then write some log:


console.log("myFields[0]",myFields[0].name,myFields[0].mapping,myFields[0].type);
console.log("myFields[1]",myFields[1].name,myFields[1].mapping,myFields[1].type);
console.log("myFields[3]",myFields[3].name,myFields[3].mapping,myFields[3].type);


the result is always
myFields[0] testname testname string
myFields[1] passrate passrate float
myFields[3] date2 date2 Object

for the old fields , the type is string/float, but for the one I create, type is an object, although the object type is a string, I'm not sure if it's ok or not.

am I right on the Create?

mitchellsimoens
10 Jan 2012, 7:17 AM
Are you wanting to add fields after the model is created or something? Ext.String is not a valid field type.

evilight
10 Jan 2012, 8:15 AM
Yes, I try to define a field dynamically.

so, what's the right solution??

mitchellsimoens
10 Jan 2012, 8:44 AM
Inertnally this is how we create the mixed collection fields:


fieldsMixedCollection.add(new Ext.data.Field(fields[i]));

fields is the array of config objects.

evilight
11 Jan 2012, 12:47 AM
this make things more complex..
I just want to new an instance of Ext.data.Field and push it into the myFields array.

now I'm totally lost.

börn
11 Jan 2012, 9:04 AM
I think you wouldn't get this working... if you want to build a dynamic model e.g. because of some fields you receive from the server. The problem is that there is no way to push fields (whether config objects or the instances) to the model class, because the fields property is a property of the model instance. If you instantiate a model with new or Ext.create you make a concrete model - after that I dont think it'll work to push your fields in it? Because when using a store with a model you would hook between the reader before the store will generate its "records" of model objects.

I made this by defining a model inline in a function after retrieving my metadata from the server (with the dynamic fields) and passing the fields config array to the Ext.define config object. After that I create a store and pass the model name to it... then it worked...

mitchellsimoens
11 Jan 2012, 9:08 AM
You should be able to change the prototype and have it affect the instances when created then.

börn
12 Jan 2012, 12:56 AM
Ah yes of course! the prototype contains the fields... that makes it even more simple! ;) thanks!