1. #1
    Sencha Touch Premium User
    Join Date
    Oct 2010
    Location
    Portland, Maine USA
    Posts
    66
    Vote Rating
    1
    gordonjl is on a distinguished road

      0  

    Default Answered: Correct Way To Bind a Record to a Form?

    Answered: Correct Way To Bind a Record to a Form?


    All,

    How do you bind a record to a form such that changes to the form fields modify the model?

    I see how you can call
    Code:
    form.setRecord(myRecord);
    , but that only manages getting the data out of the record into the form. Form -> record seems trickier, or at leas I see no automagical way to do it.

    The form documentation indicates that you can easily bind listeners to your form fields to auto-update on change, but it doesn't seem to be working. The part that isn't working is here:

    Code:
    var form = Ext.create('Ext.form.Panel', {
        listeners: {
            '> field': {
                change: function(field, newValue, oldValue) {
                    ed.set(field.getName(), newValue);
                }
            }
        },
        items: //as before
    });
    am i doing something weird, or does the above work for everyone? In the meantime, I've written this function in the form's initializer that does the same thing, but not nearly so elegantly.

    Code:
        initialize: function()    {
            Ext.each(this.query('> fieldset field'), function(field)
            {
                field.on('change', function(field, newValue, oldValue)
                {
                    this.getRecord().set(field.getName(), newValue);
                }, this);
            }, this);
        }
    Is there a better way?

    Thanks!

  2. You can use the updateRecord method on the form panel. This will go through all your fields and try to set the field value onto the record field. This updateReocrd method for some reason is not documented anymore, it was documented in past releases so I'm going to open a bug for that. Usage would be:

    Code:
    formpanel.updateRecord(formpanel.getRecord());

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,647
    Vote Rating
    899
    Answers
    3575
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    You can use the updateRecord method on the form panel. This will go through all your fields and try to set the field value onto the record field. This updateReocrd method for some reason is not documented anymore, it was documented in past releases so I'm going to open a bug for that. Usage would be:

    Code:
    formpanel.updateRecord(formpanel.getRecord());
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha Touch Premium User
    Join Date
    Oct 2010
    Location
    Portland, Maine USA
    Posts
    66
    Vote Rating
    1
    gordonjl is on a distinguished road

      0  

    Default


    If the documentation for forms':

    Code:
    var form = Ext.create('Ext.form.Panel', {
        listeners: {
            '> field': {
                change: function(field, newValue, oldValue) {
                    ed.set(field.getName(), newValue);
                }
            }
        },
        items: //as before
    });
    is wrong, would you add a bug to fix that also?

    Thanks, Mitchell!

    John

Thread Participants: 1

Tags for this Thread