开发者

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

开发者 https://www.devze.com 2023-03-14 04:42 出处:网络
I\'m using 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 li

I'm using 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:

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:

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


If you declare your XTemplate explicitly you can use the last constructor parameter which accepts a configuration object where you can specify template functions. These functions can be called with the value:function syntax.

Your code will become:

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");
        }
    })
});

These functions are executed in the scope of the XTemplate and can also be called within tpl tags or in the square bracket notation:

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

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

精彩评论

暂无评论...
验证码 换一张
取 消