1. #1
    Sencha User
    Join Date
    Jul 2010
    Location
    Austin, TX
    Posts
    3
    Vote Rating
    0
    woram is on a distinguished road

      0  

    Default Answered: Populating a form with multiple associated models

    Answered: Populating a form with multiple associated models


    I am trying to take advantage of Sencha Touch 2's ability to handle associated models. In my case, suppose I have a Selection and Book: Selection <<--> Book. So, I have a record that could look like this:

    Code:
    {
        id: 123,
        position: 1,
        book: {
            title: 'War and Peace'
        }
    }
    Suppose I want to populate a FormPanel with a Selection record. When it's a flat record, there's lots of documentation about this on the Internet, and I've gotten it to work easily:

    Code:
    myFormPanel.setRecord(record);
    When a form covers two associated models at once, this no longer works. A field dedicated to, say, book.title remains empty:

    Code:
    {
        xtype: 'textfield',
        name: 'title', // also tried 'book.title'
        label: 'Title'
    }
    Is there a way to populate Sencha Touch 2 forms automatically when the record is not flat? I could, of course, create a flat model specifically for this form, but that sort of defeats the purpose.
    If there's no automated way, what's the next best alternative? Individually fetching fields from the FormPanel and setting their values?
    Last edited by woram; 7 Jan 2013 at 8:52 PM. Reason: code tags got lost

  2. You won't be able to do it that way. What you will need to do is create a flat object using the getters from the association so you get something like

    Code:
    {
        firstname : '....',
        lastname : '....'
    }

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,548
    Vote Rating
    873
    Answers
    3566
    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 won't be able to do it that way. What you will need to do is create a flat object using the getters from the association so you get something like

    Code:
    {
        firstname : '....',
        lastname : '....'
    }
    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 User
    Join Date
    Jul 2010
    Location
    Austin, TX
    Posts
    3
    Vote Rating
    0
    woram is on a distinguished road

      0  

    Default


    Quote Originally Posted by mitchellsimoens View Post
    You won't be able to do it that way. What you will need to do is create a flat object using the getters from the association
    I'm not sure what to do with such an object. setRecord() requires a Record object derived from a model. Is there a way I'm not aware of to apply a generic object's data to a FormPanel?

  5. #4
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,548
    Vote Rating
    873
    Answers
    3566
    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


    Use setValues method
    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.

  6. #5
    Sencha User
    Join Date
    Jul 2010
    Location
    Austin, TX
    Posts
    3
    Vote Rating
    0
    woram is on a distinguished road

      0  

    Default


    Oh, wow! I wish I had known about this weeks ago. Thanks.