Results 1 to 5 of 5

Thread: Populating a form with multiple associated models

  1. #1
    Sencha User
    Join Date
    Jul 2010
    Location
    Austin, TX
    Posts
    3
    Vote Rating
    0
      0  

    Default 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 - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,551
    Answers
    3931
    Vote Rating
    1271
      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 @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  4. #3
    Sencha User
    Join Date
    Jul 2010
    Location
    Austin, TX
    Posts
    3
    Vote Rating
    0
      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 - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,551
    Answers
    3931
    Vote Rating
    1271
      0  

    Default

    Use setValues method
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  6. #5
    Sencha User
    Join Date
    Jul 2010
    Location
    Austin, TX
    Posts
    3
    Vote Rating
    0
      0  

    Default

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

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
  •