如何调用XTemplate中的函数(itemTpl)

pep*_*epe 14 javascript extjs sencha-touch extjs4 sencha-touch-2

我想在一些将输出到视图的文本上使用Ext的String方法.

例如:

itemTpl: [
    ...
    '<tpl switch="post_type">',
    '<tpl case="new_user">',
        '<p>{post_text_teaser}</p>',
        '<p>{timestamp}</p>',
    '<tpl default>',
        '<p>' + Ext.String.ellipsis( + '{post_text_teaser}' + \, 4) + '</p>',
    ...
].join(''),
Run Code Online (Sandbox Code Playgroud)

但当然第10行的连接是非法的.

你知道它是否可能或如何正确地做到这一点?

zel*_*xir 18

这应该可以解决您的问题:

    '<tpl switch="post_type">',
        '<tpl case="new_user">',
            '<p>{post_text_teaser}</p>',
            '<p>{timestamp}</p>',
        '<tpl default>',
            '<p>{[Ext.String.ellipsis(values.post_text_teaser,4,false)]}</p>',
    '</tpl>'
Run Code Online (Sandbox Code Playgroud)

您可以在Sencha Docs找到有关XTemplate的更多信息

具有模板成员函数的东西是据我所知你不能以常规方式直接在itemTpl中定义它们,但是需要显式定义一个新的XTemplate然后在你的itemTpl中使用它.见例子:

var tpl = new XTemplate(
    '<tpl switch="post_type">',
        '<tpl case="new_user">',
            '<p>{post_text_teaser}</p>',
            '<p>{timestamp}</p>',
        '<tpl default>',
            '<p>{[this.shorten(values.post_text_teaser)]}</p>',
    '</tpl>',
    {        
        shorten: function(name){
           return Ext.String.ellipsis(name,4,false);
        }
    }
);

...

itemTpl: tpl,

...
Run Code Online (Sandbox Code Playgroud)

Senchafiddle的例子

这应该可以正常工作,如下面的代码(只需插入上面的XTemplate代码).

itemTpl: new XTemplate(...),
Run Code Online (Sandbox Code Playgroud)

Senchafiddle的例子

希望这可以解决它!

编辑注意到我错过了结束标记,有时它没有它们,但是总是使用它们是好的做法,因为它们可能会导致有趣的错误(在这种情况下,生成的代码上缺少括号).