Results 1 to 5 of 5

Thread: Ext.data.Model frequently used Fields or Mixins

  1. #1
    Sencha User
    Join Date
    Jun 2012
    Location
    Pretoria - South Africa
    Posts
    22
    Answers
    4

    Default Answered: Ext.data.Model frequently used Fields or Mixins

    Hi All,

    Any of you have recommendations or best practice for code re-use on frequently (but not universally) used fields in data models?

    I'm aware that common fields can be placed in a base model and then used in extended models, but that's often overly simplistic - what if only a few of the extended models require a certain set of fields.

    I thought of using mixins to the Ext.data.Model, but can't seem to push the additional field definitions in time:
    Code:
    Ext.define('MyApp.models.MixZ', {
        extend: 'Ext.Mixin',
        mixinConfig: {
            before: {
                constructor: 'addFields'
            }
        },
        addFields: function(){
            Ext.Array.push(this.fields, ['mixFieldZ1', 'mixFieldZ2']);
        }
    });
    
    Ext.define('MyApp.models.ModelA', {
        extend: 'Ext.data.Model',
        mixins: {
            mixZ: 'MyApp.models.MixZ'
        },
    
        fields: [ 'field1', 'field2']
    });
    Any suggestions?

  2. In case anyone stumbles across this: I found another solution. Advantages, to my mind, are that it's simpler and that the model to be mixed in remains an ordinary model class that can be instantiated on its own, too.

    PHP Code:
    Ext.define('MyApp.models.MixZ', {
        
    extend'Ext.data.Model',
        
    fields: ['mixField1''mixField2']
    });

    Ext.define('MyApp.models.ModelA', {
        
    extend'Ext.data.Model',
        
    fields: [ 'field1''field2']
    }, function() {
        
    this.addFields(MyApp.models.MixZ.fields)
    }); 

  3. #2
    Sencha - Sustaining Engineer tristan.lee's Avatar
    Join Date
    Mar 2015
    Location
    Central Ohio
    Posts
    1,579
    Answers
    165

    Default

    I'm not sure if the mixin is the best way to go. Maybe others can provide their input on that though. It seems to me having your model extend a class with these additional fields would be the easiest. It doesn't need to be a base model that all models extend. Just something specific to these few that require it.
    Tristan Lee
    Sencha Inc - Sustaining Engineer


    Having an issue? Help us help you - be detailed; provide some code; demonstrate with a fiddle (fiddle.sencha.com)

    Embed your fiddle in your post: [FIDDLE]id[/FIDDLE]

  4. #3
    Sencha User
    Join Date
    Jun 2012
    Location
    Pretoria - South Africa
    Posts
    22
    Answers
    4

    Default

    My problem with extending is the forced linear heritage. Where in the tree do I place certain fields that are selectively required across various branches?

    With some more fiddling from my previous example, I think I've kludged something together that works in my simple tests so far. Allows multiple mixins per model.

    PHP Code:
    Ext.define('MyApp.models.MixZ', {
        
    extend'Ext.Mixin',
        
    mixinConfig: {
            
    before: {
                
    constructor'addMixFields'
            
    }
        },
        
    addMixFields: function(){
            var 
    fields = ['mixField1''mixField2'];
            
    Ext.Array.push(this.self.fieldsfields);
            
    this.self.addFields(fields);
        }
    });

    Ext.define('MyApp.models.ModelA', {
        
    extend'Ext.data.Model',
        
    mixins: {
            
    mixZ'MyApp.models.MixZ'
        
    },

        
    fields: [ 'field1''field2']
    }); 
    I certainly don't know if this is the best way of doing multiple 'inheritance' for data models though?

  5. #4
    Sencha User
    Join Date
    Jun 2009
    Location
    Heidelberg, Germany
    Posts
    79
    Answers
    1

    Default

    In case anyone stumbles across this: I found another solution. Advantages, to my mind, are that it's simpler and that the model to be mixed in remains an ordinary model class that can be instantiated on its own, too.

    PHP Code:
    Ext.define('MyApp.models.MixZ', {
        
    extend'Ext.data.Model',
        
    fields: ['mixField1''mixField2']
    });

    Ext.define('MyApp.models.ModelA', {
        
    extend'Ext.data.Model',
        
    fields: [ 'field1''field2']
    }, function() {
        
    this.addFields(MyApp.models.MixZ.fields)
    }); 

  6. #5
    Sencha User
    Join Date
    Jun 2012
    Location
    Pretoria - South Africa
    Posts
    22
    Answers
    4

    Default

    Definitely looks more elegant than the Mixin solution, thanks for sharing despite the relevant age of the thread.

Similar Threads

  1. [INFOREQ] Ext.Data.Model.get() does not work for all fields
    By John2011 in forum Sencha Touch 2.x: Bugs
    Replies: 1
    Last Post: 30 Oct 2012, 5:32 AM
  2. setFields() of Ext.data.Model not able to add fields
    By mukeshgla in forum Sencha Touch 2.x: Q&A
    Replies: 2
    Last Post: 20 Jul 2012, 7:37 AM
  3. Ext.data.Model mapping unkown fields
    By Souri in forum Sencha Touch 2.x: Q&A
    Replies: 2
    Last Post: 25 May 2012, 9:31 AM
  4. Ext.data.Model - Modiying Non-convert Fields Referenced by Convert Fields.
    By jpongin in forum Sencha Touch 1.x: Discussion
    Replies: 0
    Last Post: 29 Jul 2011, 11:27 PM
  5. data.Model fields not modified (bug)?
    By taviroquai in forum Ext: Discussion
    Replies: 0
    Last Post: 29 Jun 2011, 6:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •