The Template of Ext can be used to parse the second-layer json data. The Template of Ext supports Template replacement by passing in json data.
There is an example in the API:
The Code is as follows:
Var t = new Ext. Template (
'
',
'{Name: trim} {value: ellipsis (10 )}',
'
'
);
T. append ('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar '});
Make some modifications and perform a test:
The Code is as follows:
Var t = new Ext. Template (
'
',
'{Name} {value }',
'
'
);
Var dt = t. apply ({id: 'myid', cls: 'myclass', name: 'foo', value: 'bar '});
Alert (dt );
Running the above Code will pop up.
Foo bar
It indicates that the replacement is successful.
However, if another template data is as follows:
The Code is as follows:
{Id: 'myid', cls: {o: 'myclass'}, name: 'foo', value: 'bar '}
We want to replace the original cls part of the template with the cls. o value, that is, myclass. What should we do? Do you want to use {cls. o} directly? You can try it. It is definitely invalid and has not been replaced. Because template matching and replacement directly match the string before the colon in {} with the JSON variable. Of course, the cls. o string cannot be found, so it cannot be matched.
Fortunately, the Template supports data parsing.
We can define a resolution function by ourselves. It is actually very simple:
The Code is as follows:
Var t = new Ext. Template (
'
',
'{Name} {value }',
'
'
);
T. parseJSON = function (data) {return data. o };
Var dt = t. apply ({id: 'myid', cls: {o: 'myclass'}, name: 'foo', value: 'bar '});
Alert (dt)
We have defined a parsing method called parseJSON. Access the top-level cls in the template and then process the cls (an object) value (directly access its o attribute.