PDA

View Full Version : Building Form Fields Dynamically - Help!



dac0nvu
11 Mar 2011, 5:58 AM
I'm still new to Sencha Touch and I can't seem to get my head around this problem.

I want to create a form to be used for adding new 'User' objects and editing existing 'User objects. The form would have the expected fields: id, first_name, last_name, but it also needs to contain a list of 'Roles' for each user. A user must have at least one role and can have several roles.

The roles are defined by selecting from two drop-down boxes: Application and Access. So by selecting from the application drop-down and selecting from the access drop-down, you are defining a role for a user.

I have the id, first_name, last_name fields built on my form but I can't figure out how to dynamically build the appropriate number of drop-down boxes that define the 1 to many roles that may be defined for a user when I load the 'User' into the form for editing. There will always be at least one role (meaning at least one set of Application and Access drop-downs), but what if there are two, or three, or more roles? Would I have to have some kind of loop that builds these drop-downs when call store.loadData(userData)?

My json code coming back from the server for the user object looks like this:



{
"success": true,
"user": [
{
"id": "ABC0XYZ",
"first_name": "John",
"last_name": "Smith",
"roles": [
{
"role" : [
{
"application" : "APP1",
"access" : "R"
}
]
},
{
"role" : [
{
"application" : "APP2",
"access" : "W"
}
]
}
]
}
]
}


And this is what I was thinking my User model should look like:



Ext.regModel('Role', {
fields: [
{name: 'application', type: 'string'},
{name: 'access', type: 'string'}
]
});

Ext.regModel('User', {
fields: [
{name: 'id', type: 'string'},
{name: 'first_name', type: 'string'},
{name: 'last_name', type: 'string'}
],
hasMany: ['roles', {model: 'Role', name: 'roles'}]
})


Anyone have any ideas on how to build a form panel to handle the dynamic number of roles that a User could have ?