PDA

View Full Version : why my eval(json) dose not work?



cntop@msn.com
6 Nov 2012, 8:09 PM
due to the dynamic module consideration, i must output ext code in json block.

this time i want to add combobox into a grid column dynamically, the extjs code fragment like this


var grid = Ext.create('Ext.grid.Panel', {
...
store: store,
columns: eval(myJson.columnModel),
width: 95,
...
})

"myJson" had gotten from Ext.Ajax.request Correctly.

the json code come from server like this


"columnModel": [
{
"head": "testnum",
"dataIndex": "number",
"editor": "Ext.create('Ext.form.field.ComboBox',{typeAhead: true,triggerAction: 'all',selectOnTab: true,store: [['1','admin'],['2','demo'],['3','member'],['4','leader'],['35','cd1'],['36','1333'],['37','111'],['38','www'],['39','1221']],lazyRender: true,listClass: 'x-combo-list-small'})"
}


but the console report error "Error: [Ext.createByAlias] Cannot create an instance of unrecognized alias: widget.Ext.create('Ext.form.field.ComboBox',{......"

"columns: eval(myJson.columnModel)" does not work, why?
thx for your help.

vietits
6 Nov 2012, 10:42 PM
That's because your eval(myJson.columnModel) will generate the same value of myJson.columnModel and the editor config will be "Ext.create('Ext.form.field.ComboBox', {...})". This will be considered an alias (xtype) to create field editor and this is of course an invalid xtype.


[{
"head": "testnum",
"dataIndex": "number",
"editor": "Ext.create('Ext.form.field.ComboBox',{typeAhead: true,triggerAction: 'all',selectOnTab: true,store: [['1','admin'],['2','demo'],['3','member'],['4','leader'],['35','cd1'],['36','1333'],['37','111'],['38','www'],['39','1221']],lazyRender: true,listClass: 'x-combo-list-small'})"},{
...
]

To avoid this, the value of your myJson.columnModel should be a string, like below:


var myJson = {
"columnModel" = "[{header:'testnum', dataIndex: 'number', editor: Ext.create('Ext.form.field.ComboBox', {...})}, {...}]"
}

cntop@msn.com
7 Nov 2012, 4:11 PM
thank you, @vietits (http://www.sencha.com/forum/member.php?362966-vietits).
due to my server side program is PHP, and the json is a transform from php array, so its difficult to remove double quotation marks, so i put a special mark in array like this


$arr = array("editor"=>"#splitstart#Ext.create('Ext.form.field.ComboBox',{typeAhead: true,triggerAction: 'all',selectOnTab: true,store: [".$storeData."],lazyRender: true,listClass: 'x-combo-list-small'})#splitend#");
$jsonstr = json_decode($arr);
//code here to remove double quotation marks in ahead of #splitstart#
//code here to remove double quotation marks in back of #splitend#
echo $jsonstr;

Tim Toady
7 Nov 2012, 9:04 PM
Gross. I doubt you need to explicitly use eval. At the very least you could do


$arr = array(
...
"columnModel" => array(


array(


'text' => 'testNum',
'dataIndex' => 'number',
'editor' => array(


'xtype' => 'combo',
'typeAhead' => true,
...


)


)


)
);
echo json_encode( $arr );



Then it will render the editor when it needs it and you don't need to run an Ext.create in it. You can decode the whole json string using Ext.decode.

Generally when I am working where the columns can change based on data from the server I use the onMetaChange event with metadata and grid.reconfigure.

cntop@msn.com
8 Nov 2012, 12:31 AM
thx a lot, Tim.
my brain has low speed recently, i see light suddenly that i can use 'xtype:combobox' replace 'ext.create()', the xtype syntax can avoid double quotation marks.