1. #1
    Sencha User
    Join Date
    Mar 2011
    Posts
    26
    Vote Rating
    3
    FanOfExt is on a distinguished road

      0  

    Default 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.

    Code:
    Ext.define('CVM.model.ListDetailMod', {
        extend: 'Ext.data.Model',
        config:{
            fields:['ItemId'],
            idProperty:'ItemId'
        }
    });
    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.

    I found one post recommending this approach:

    Code:
    var myModel = Ext.ModelManager.getModel('CVM.model.ListDetailMod');
    myModel.prototype.fields.add(new Ext.data.Field("Id"));
    However it did not work for me!


    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).

    Code:
    var fieldsArray = new Array();
    fieldsArray[0]="ItemId";
    fieldsArray[1]="Id";
    
    var model = Ext.ModelManager.getModel('CVM.model.ListDetailMod');
    model.setFields(fieldsArray);
    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".


    Thanks for the help.

  2. #2
    Sencha User bluehipy's Avatar
    Join Date
    Mar 2010
    Location
    Romania
    Posts
    608
    Vote Rating
    26
    Answers
    66
    bluehipy will become famous soon enough bluehipy will become famous soon enough

      0  

    Default


    You want to add fields to a class or to an instance model?
    Latest thoughts on the subject: http://joy2share.com/senchatouch/

  3. #3
    Sencha User
    Join Date
    Mar 2011
    Posts
    26
    Vote Rating
    3
    FanOfExt is on a distinguished road

      0  

    Default


    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

  4. #4
    Sencha User bluehipy's Avatar
    Join Date
    Mar 2010
    Location
    Romania
    Posts
    608
    Vote Rating
    26
    Answers
    66
    bluehipy will become famous soon enough bluehipy will become famous soon enough

      0  

    Default


    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/

  5. #5
    Ext JS Premium Member
    Join Date
    Sep 2010
    Posts
    370
    Vote Rating
    6
    Answers
    3
    stewardsencha is on a distinguished road

      0  

    Default


    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?

Thread Participants: 2

Tags for this Thread