Enables ext's template to parse the two-layer JSON data method _json

Source: Internet
Author: User
Ext's template supports template replacement by passing in JSON data.
An example of this is the API:

Copy Code code as follows:

var t = new Ext.template (
' <div name= ' {id} ' > ',
' <span class= ' {cls} ' >{name:trim} {value:ellipsis (#)}</span> ',
' </div> '
);
T.append (' Some-element ', {id: ' myID ', cls: ' MyClass ', Name: ' foo ', Value: ' Bar '});



Make a little change to do a test:

Copy Code code as follows:

var t = new Ext.template (
' <div name= ' {id} ' > ',
' <span class= ' {cls} ' >{name} {value}</span> ',
' </div> '
);
var dt=t.apply ({id: ' myID ', cls: ' MyClass ', Name: ' foo ', Value: ' Bar '});
alert (DT);


Running the code above will pop up <div name= "myID" ><span class= "MyClass" >foo bar</span></div> to describe the replacement success.

But if you have such a template data:

Copy Code code as follows:

{ID: ' myID ', Cls:{o: ' MyClass '}, Name: ' foo ', Value: ' Bar '}



We want to replace the central part of the template with the CLS portion of the CLS.O value, that is, MyClass, what do we do? is not want to use {CLS.O} directly, you can try, absolutely invalid, no replacement. Because the template matching substitution is directly matching the string preceding the colon in {} to the JSON variable. Of course I can't find cls.o this string so I can't match it.
Fortunately, template supports the parsing of data.
We can define an analytic function ourselves. It's actually very simple:


Copy Code code as follows:

var t = new Ext.template (
' <div name= ' {id} ' > ',
' <span class= ' {Cls:this.parseJSON} ' >{name} {value}</span> ',
' </div> '
);
T.parsejson=function (data) {return DATA.O};
var dt=t.apply ({id: ' myID ', cls: {o: ' MyClass '}, Name: ' foo ', Value: ' Bar '});
Alert (DT)



We define a parsing method called Parsejson, which accesses the top-level CLS in the template and then processes the value of the CLS (which is an object) (directly accessing its O property).

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.