Sencha / javascript - how to call a function from inside a tpl template

I am using Sencha touch, and I am trying to modify the Twitter example that I found on the Internet to turn Twitter URLs into interactive links. I saw that one of the examples in the sencha touch library uses the bind function, but I cannot figure out how to include it in my own project. Here is my 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 is the error:

Uncaught TypeError: Object [object Object] has no method 'linkify'
+3
source share
1 answer

If you declare your XTemplate explicitly, you can use the last constructor, which takes a configuration object, where you can specify template functions. These functions can be called using the value: syntax.

Your code will be as follows:

t_news = new Ext.Component({
cls:'t_news',
title:'News',
scroll: 'vertical',
tpl: new Ext.XTemplate(
    '<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>',
    {
        linkify: function(value){
            return value.replace(/(http:\/\/[^\s]*)/g, "$1");
        }
    })

});

XTemplate tpl :

'<tpl if="this.linkify(values.text) == \'some text\'">',
'</tpl>'

'<p>{[this.linkify(values.text)]}</p>'

, !

+13

All Articles