-
15 Nov 2011 6:52 PM #1
Unanswered: Passing data to an event listener in an XTemplate
Unanswered: Passing data to an event listener in an XTemplate
Hi all,
just thought I'd post a technique I use to pass data to an event listener on an XTemplate.
xTemplate
ListenerCode:tpl : new Ext.XTemplate( '<img class="ImageClass" dataAttributeName1="{dataAttributeValue1}" dataAttributeName2="{dataAttributeName2}" src="http://domain.com/myimage.jpg" border="0"/>')
The values {dataAttributeValue1} and {dataAttributeValue2} will come from some JSON that you will append to the template. Eg this.tpl.append(SomeContainer, JSONitems);Code:listeners : { el: { click: function (ctl) { var dataAttributeValue1= ctl.target.attributes['dataAttributeName1'].nodeValue; var dataAttributeName2= ctl.target.attributes['dataAttributeValue2'].nodeValue; }, delegate: ".ImageClass" } }
-
17 Nov 2011 12:07 PM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,714
- Vote Rating
- 438
- Answers
- 3113
How well have you tested this? What happens when the JSON has double-quotes? Or do spaces in the JSON break it?
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
18 Nov 2011 1:06 AM #3
Escaping JSON values
Escaping JSON values
Hi Mitchell,
I've tested this in Chrome, Safari and mobile Safari. All work with no problems. My data is pretty straight forward but if anyone has needs to escape/encode text or JSON values then you could use an XTemplate member function. Your data can then be manipulated before being placed inline. Eg:
CheersCode:tpl : new Ext.XTemplate( '<img class="ImageClass" dataAttributeName1="{dataAttributeValue1:this.myFunction}" dataAttributeName2="{dataAttributeValue2:this.myFunction}" src="http://domain.com/myimage.jpg" border="0"/>', { myFunction : function(val){ return Ext.util.JSON.encode(val); }, });
Francesco


Reply With Quote