1. #1
    Sencha User
    Join Date
    Dec 2010
    Posts
    29
    Vote Rating
    20
    Answers
    2
    francescosyd is on a distinguished road

      0  

    Default 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

    Code:
     tpl : new Ext.XTemplate(
                        '<img  class="ImageClass" dataAttributeName1="{dataAttributeValue1}" dataAttributeName2="{dataAttributeName2}" src="http://domain.com/myimage.jpg" border="0"/>')
    Listener

    Code:
    listeners : {
                        el: {
                            click: function (ctl) {
                                    var dataAttributeValue1= ctl.target.attributes['dataAttributeName1'].nodeValue;
                                    var dataAttributeName2= ctl.target.attributes['dataAttributeValue2'].nodeValue;
                                    
                            },
                            delegate: ".ImageClass"
                        }
                }
    The values {dataAttributeValue1} and {dataAttributeValue2} will come from some JSON that you will append to the template. Eg this.tpl.append(SomeContainer, JSONitems);

  2. #2
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    38,412
    Vote Rating
    1103
    Answers
    3682
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default

    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 Software Engineer
    ________________
    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 in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Dec 2010
    Posts
    29
    Vote Rating
    20
    Answers
    2
    francescosyd is on a distinguished road

      0  

    Default Escaping JSON values

    Quote Originally Posted by mitchellsimoens View Post
    How well have you tested this? What happens when the JSON has double-quotes? Or do spaces in the JSON break it?
    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:

    Code:
    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);
        },
    });
    Cheers

    Francesco

Thread Participants: 1