-
9 Feb 2013 2:15 PM #1
Unanswered: Added fields to Model dynamically in touch2
Unanswered: Added fields to Model dynamically in touch2
Hi,
I've searched this form and online extensively looking for a way to add fields to my model dynamically. I found many posts on the topic but no concrete working solution.
This is my model, I only know one field upfront, the rest I have to set based on a configuration that I retrieve.
No I cannot define all the fields upfront, it must be dynamic. By the way I got this working in an extjs 4 app so I'm confused as to why it isnt working with touch2.Code:Ext.define('CVM.model.ListDetailMod', { extend: 'Ext.data.Model', config:{ fields:['ItemId'], idProperty:'ItemId' } });
I found one post recommending this approach:
However it did not work for me!Code:var myModel = Ext.ModelManager.getModel('CVM.model.ListDetailMod'); myModel.prototype.fields.add(new Ext.data.Field("Id"));
Why cant I just do something like this? (which does not work by the way, but I found a slight variation worked for my grid model in extjs4).
In both scenarios I know they dont work because the records are not populated by my store. However if I hard code the one "ItemId" field on my model and try to set the rest dynamically I do see data for "ItemId" but not for any of the other ones. If I set all fields on my model dynamically including "ItemId" then my store records dont have any data i.e. when I do store.getAt(0).get("ItemId") I get "undefined".Code:var fieldsArray = new Array(); fieldsArray[0]="ItemId"; fieldsArray[1]="Id"; var model = Ext.ModelManager.getModel('CVM.model.ListDetailMod'); model.setFields(fieldsArray);
Thanks for the help.
-
10 Feb 2013 4:41 AM #2
You want to add fields to a class or to an instance model?
Latest thoughts on the subject: http://joy2share.com/senchatouch/
-
10 Feb 2013 4:53 AM #3
Hi,
Sorry I'm rather new to ext and touch so I'm not sure what exactly either of those are.
Basically I define a model without fields in a js file. I also have a store with reader and proxy in another js file.
At runtime I want to add fields to the model used by my store because I retrieve a set of records into my store with fields that I only know at runtime.
Now that I think about it, I think I'm trying to add fields to an instance model. Class would mean in the Js file. Is that correct?
Thank you
-
10 Feb 2013 7:07 AM #4
Ext.define will create a "class" definition, Ext.create will create an instance.
I think you can create the definition of the model also at run time, and with some tweaking of a custom reader/prtoxy/store you can create the model as you need it.
Some inspiration:
Ext.define('User', {
extend: 'Ext.data.Model',
config:{
fields: ['name','age','phone']
},
addField:function(field){
var fields = this.getFields().items || [];
var data = this.getData(); // preserve data
fields.push(field);
this.setFields( fields );
this.setData( data );
}
});
var user = Ext.create('User',{name:'blue', age:35, phone:'0723538xxx' } );
user.addField({name:'caffee', type:'string'});
user.set('caffee','Arabica');
console.log( user.get('caffee') );
console.log(user.getData() );Latest thoughts on the subject: http://joy2share.com/senchatouch/
-
8 Apr 2013 3:50 PM #5
So it seems there are two approaches.
Build the definition then instatiate it.
or
Add field to an instance (getFields/setFields).
So I wonder...
All other things being equal, which gives the best performance?
All other things being equal, are there any strong reasons to prefer one over the other?


Reply With Quote