1. #1
    Sencha User
    Join Date
    Jun 2011
    Posts
    10
    Vote Rating
    0
    heaversm is on a distinguished road

      0  

    Default Calling a function inside a tpl [ ]

    Calling a function inside a tpl [ ]


    Hi - I'm new to Sencha touch and I'm trying to modify a twitter example I found online in order to turn urls from a twitter feed into clickable links. I saw that one of the examples in the sencha touch library uses a linkify feature, but I can't figure out how to incorporate it into my own project. Here's my code:

    Code:
    t_news = new Ext.Component({
    			cls:'t_news',
    			title:'News',
    			scroll: 'vertical',
    			tpl: [
    				'<tpl for=".">',
    					'<div class="tweet">',
    							'<div class="avatar"><img src="{profile_image_url}" /></div>',
    							'<div class="tweet-content">',
    								'<h2>{from_user}</h2>',
    								'<p>{text:this.linkify}</p>',
    							'</div>',
    					'</div>',
    				'</tpl>',
    			]
    		});
    
    function linkify(value){
            	return value.replace(/(http:\/\/[^\s]*)/g, "<a target=\"_blank\" href=\"$1\">$1</a>");
    		}
    and here's the error I get:

    Code:
    Uncaught TypeError: Object [object Object] has no method 'linkify'

  2. #2
    Sencha User gaurav.k's Avatar
    Join Date
    Nov 2011
    Location
    Bangalore, India
    Posts
    11
    Vote Rating
    0
    gaurav.k is on a distinguished road

      0  

    Lightbulb use {[... ]}

    use {[... ]}


    Here is a sample code,
    hope it helps.

    Code:
    var sbTpl = new Ext.XTemplate('<tpl for=".">',
     '<div class="sb-section-title">{title}</div>',
     '<div class="sb-section-score">{correct} of {total} Correct &nbsp;(&nbsp;{[getPercent(values.correct,values.total)]}&nbsp;%)</div>',
    '</tpl>');

    " You can add any javascript code inside {[ ... ]}"
    Last edited by gaurav.k; 16 Dec 2011 at 3:54 AM. Reason: typographical mistake

  3. #3
    Sencha - Community Support Team mankz's Avatar
    Join Date
    Nov 2007
    Location
    Stockholm, Sweden
    Posts
    2,777
    Vote Rating
    112
    mankz is a name known to all mankz is a name known to all mankz is a name known to all mankz is a name known to all mankz is a name known to all mankz is a name known to all

      0  

    Default


    Linkify needs to be defined on the template itself, not on the class.

    Code:
    			tpl: [
    				'<tpl for=".">',
    					'<div class="tweet">',
    							'<div class="avatar"><img src="{profile_image_url}" /></div>',
    							'<div class="tweet-content">',
    								'<h2>{from_user}</h2>',
    								'<p>text:{[this.linkify(values.text)]}</p>',
    							'</div>',
    					'</div>',
    				'</tpl>',
    {
        linkify : function (value){
            	return value.replace(/(http:\/\/[^\s]*)/g, "<a target=\"_blank\" href=\"$1\">$1</a>");
    		}
    }
    			]
    		});

Thread Participants: 2

Tags for this Thread