Results 1 to 2 of 2

Thread: Model: Correct mapping to populate a form textfield from an array of objects?

  1. #1
    Sencha User
    Join Date
    Jan 2017
    Posts
    2
    Vote Rating
    0
      0  

    Default Model: Correct mapping to populate a form textfield from an array of objects?

    Context: My JSON contains an array and I have to "flatten" it to put data in the form view.
    The line in blue is how I do it:

    JSON received from server containing an array
    Code:
                    names: [{
                        name: "first",
                        value: "John"
                    },{
                        name: "last",
                        value: "DOE"
                    }],



    My Form panel

    Code:
            items: [
                {
                    xtype: 'fieldset',
                    items: [
                        {
                            xtype: 'textfield',
                            label: 'Field',
                            name: 'first_name'
                        }
                    ]
                }
    My Model
    Code:
    Ext.define('Test.model.Person', {
        extend: 'Ext.data.Model',
    
    
        requires: [
            'Ext.data.Field',
            'Ext.data.association.HasMany'
        ],
        uses: [
            'Test.model.Name'
        ],
    
    
        config: {
            fields: [
                {
                    mapping: 'names.filter(function(obj){return obj.name == "first"; })[0].value;',
                    name: 'first_name',
                    type: 'string'
                }
            ],
            hasMany: {
                model: 'Test.model.Name'
            }
        }
    });

    IT WORKS: the form is displayed with the `John` value in the `first_name` textfield. However my question is: it is the right way to maps arrays to forms? Or is there a better way?

  2. #2
    Sencha User
    Join Date
    Mar 2017
    Posts
    2
    Vote Rating
    0
      0  

    Default

    Koopatroopa how are you? can you put an example about how you do an objects array in a form? i need something like this:

    "address" : {
    "address" : "Avenida Madison",
    "city" : "New York, USA"
    }

    thats a part of a json file, but i dont know how to put this Adress and city value inside the Address array

  3. #3
    Sencha User
    Join Date
    Mar 2017
    Posts
    2
    Vote Rating
    0
      0  

    Default

    this is my entire json file:

    {
    "id"
    : 1,
    "name": "Acrecer",
    "identification": "963.654.988",
    "email": "prueba2@alegra.com",
    "phonePrimary": "111 11 11",
    "phoneSecondary": "",
    "fax": "",
    "mobile": "(333) 555-55-55",
    "observations": "",
    "type" : ["client"],
    "address" : {
    "address" : "Avenida Madison",
    "city" : "New York, USA"
    },
    "seller": {
    "id" : "52",
    "name" : "Diana Giraldo",
    "identification" : 560,
    "observations" : 320
    },
    "term" :
    {
    "id" : 2,
    "name" : "8 días",
    "days" : 8
    },
    "priceList" : null,
    "internalContacts" : [
    {
    "id" : 1,
    "name" : "Lina",
    "lastName" : "Montoya",
    "email" : "prueba4@alegra.com",
    "phone" : "123 45 67 ext 25",
    "mobile" : "(333) 333 33 33",
    "sendNotifications" : "no"
    }
    ]
    }

Similar Threads

  1. Mapping form to a nested model
    By whizzer in forum Ext: Q&A
    Replies: 1
    Last Post: 30 Jul 2014, 6:44 PM
  2. Mapping model fields to Object with array values
    By msfounder in forum Sencha Touch 2.x: Q&A
    Replies: 1
    Last Post: 10 Jul 2014, 4:14 AM
  3. Replies: 8
    Last Post: 26 Jun 2014, 3:10 PM
  4. Model/Store Mapping with an array
    By Ant0nin in forum Sencha Touch 2.x: Q&A
    Replies: 3
    Last Post: 29 Jan 2014, 12:56 AM
  5. Replies: 1
    Last Post: 22 May 2013, 9:05 AM

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
  •