Method _ json

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.