Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7708453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:36:56+00:00 2026-06-01T00:36:56+00:00

Let’s say I have a model called Vehicle Ext.define(‘AM.model.Vehicle’, { extend: ‘Ext.data.Model’, fields: [

  • 0

Let’s say I have a model called Vehicle

Ext.define('AM.model.Vehicle', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'brandId',  type: 'int'}, 
        {name: 'brandName',  type: 'string'},
        {name: 'modelId',  type: 'int'}, 
        {name: 'modelName',  type: 'string'},
        {name: 'yearOfRelease',  type: 'int'}
    ]

});

As you can see, the vehicle model has fields “brandId”, “brandName”, etc.
For example, I want to edit an instance of this model.
I create an edit form, which has combobox linked to ‘brandId’ field.
Then I save the form values to a model instance with this

values = form.getValues();
record.set(values);

It all works fine, but there is a problem with all the fields that represent some outer models:
only id’s are updated, while all other fields, that depend on id remain the same.

In a regular OOP language(Java for example), I would create a class CarBrand
and put an instance of this class inside the Vehicle class.
In ExtJs 4 they have hasMany relationship, but don’t have hasOne.

What is the best approach in ExtJS 4 for such nested models?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:36:58+00:00Added an answer on June 1, 2026 at 12:36 am

    4.0.* doesn’t support the hasOne relationship. 4.1 is meant to support it.

    for 4.0.7 I have the following override in place.

    Ext.define('HOD.overrides.Form', {}, function() {
    
    /*
     * Implementing a nested setValues for forms with
     * arrays in them.
     */
    Ext.override(Ext.form.Basic, {
        setValues: function(values, arrayField) {
            var me = this;
    
            function setVal(fieldId, val) {
                if (arrayField) {
                    fieldId = arrayField + '.' + fieldId;
                }
                var field = me.findField(fieldId);
                if (field) {
                    field.setValue(val);
                    if (me.trackResetOnLoad) {
                        field.resetOriginalValue();
                    }
                } else if(Ext.isObject(val)) {
                    me.setValues(val, fieldId);
                }
            }
    
            if (Ext.isArray(values)) {
                // array of objects
                Ext.each(values, function(val) {
                    setVal(val.id, val.value);
                });
            } else {
                // object hash
                Ext.iterate(values, setVal);
            }
            return this;
        },
        /**
         * Persists the values in this form into the passed {@link Ext.data.Model} object in a beginEdit/endEdit block.
         * @param {Ext.data.Model} record The record to edit
         * @return {Ext.form.Basic} this
         */
        updateRecord: function(record) {
            var values = this.getFieldValues(),
            name,
            obj = {};
    
            function populateObj(record, values) {
                var obj = {},
                name;
    
                record.fields.each(function(field) {
                    name = field.name;
                    if (field.model) {
                        var nestedValues = {};
                        var hasValues = false;
                        for(var v in values) {
                            if (v.indexOf('.') > 0) {
                                var parent = v.substr(0, v.indexOf('.'));
                                if (parent == field.name) {
                                    var key = v.substr(v.indexOf('.') + 1);
                                    nestedValues[key] = values[v];
                                    hasValues = true;
                                }
                            }
                        }
                        if (hasValues) {
                            obj[name] = populateObj(Ext.create(field.model), nestedValues);
                        }
                    } else if (name in values) {
                        obj[name] = values[name];
                    }
                });
                return obj;
            }
    
            obj = populateObj(record, values);
    
            record.beginEdit();
            record.set(obj);
            record.endEdit();
    
            return this;
        }
    });
    

    Whats this lets me do is in my forms I create them with names like so.

    // Contact details
    {
        xtype: 'fieldcontainer',
        defaultType: 'textfield',
        layout: 'anchor',
        fieldDefaults: {
            anchor: '80%',
            allowBlank: true
        },
        items: [
    
        {
            xtype: 'textfield',
            name: 'homePhone',
            fieldLabel: 'Home phone number'
        },
        {
            xtype: 'textfield',
            name: 'mobilePhone',
            fieldLabel: 'Mobile phone number'
        }]
    },
    {
        xtype: 'fieldcontainer',
        defaultType: 'textfield',
        layout: 'anchor',
        fieldDefaults: {
            anchor: '80%',
            allowBlank: true
        },
        items: [
        {
            name: 'address.id',
            xtype: 'hidden'
        },
        {
            name: 'address.building',
            fieldLabel: 'Building'
        },
        {
            name: 'address.street',
            fieldLabel: 'Street'
        },
        {
            name: 'address.city',
            fieldLabel: 'City'
        },
        {
            name: 'address.county',
            fieldLabel: 'County'
        },
        {
            name: 'address.postcode',
            fieldLabel: 'Postcode'
        },
        {
            name: 'address.country',
            fieldLabel: 'Country'
        }
        ]
    },
    ]
    

    Notice the . in the name field which lets the overridden setValues and updateRecord form know it needs to map these values to the new model, which is defined in the model like so.

    Ext.define('HOD.model.Employee', {
        extend: 'Ext.data.Model',
        fields: [
            // Person Class
            // Auto
            'id',
            'name',
            'homePhone',
            'mobilePhone',
            {model: 'HOD.model.Address', name: 'address'}, //Address
            {model: 'HOD.model.Contact', name: 'iceInformation'} //Person
        ]
    });
    
    Ext.define('HOD.model.Address', {
        extend: 'Ext.data.Model',
        fields: [
            'building',
            'street',
            'city',
            'county',
            'postcode',
            'country'
        ],
        belongsTo: 'Employee'
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
let's assume, I have a model called Chicken: class Chicken(models.Model): name = models.CharField(max_length=30) and
Let's say I'm building a data access layer for an application. Typically I have
Let's say I have window.open (without name parameter), scattered in my project and I
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's say I have an image called hello.png with dimensions 200x100 . I create
Let's say I have the following SQL query SELECT id, name, title, description, time
Let's say I have the following code: <ul> <li data-val=1>1</li> <li data-val=4>2</li> <li data-val=2>3</li>
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.