如何调用XTemplate(itemTpl)中的函数 [英] How to call functions within a XTemplate (itemTpl)

查看:219
本文介绍了如何调用XTemplate(itemTpl)中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Ext的字符串一些文本的方法将输出到视图。

I would like to use Ext's String method on some text that will be output to the view.

例如:

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(''),

但当然,第10行中的连接是非法的。

but of course the concatenation in line 10 is illegal.

您是否知道是否可能或如何正确执行?

Do you know if it's possible or how to do this correctly?

推荐答案

解决你的问题:

    '<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>'

您可以在 Sencha文档

模板成员函数的事情是,据我所知您不能以常规方式直接在itemTpl中定义它们,但需要明确定义一个新的XTemplate,然后在您的itemTpl中使用它。参见示例:

The thing with template member function is that as far as I know you cannot define them directly in the itemTpl in the regular way, but need to explicitly define a new XTemplate and then use that in your itemTpl. See example:

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,

...

Senchafiddle示例

这样就可以正常工作,如下面的代码(只需插入上面的XTemplate的代码)。

This should work fine as will the code below (just insert the code from the XTemplate above).

itemTpl: new XTemplate(...),

Senchafiddle示例

希望这样排序!

编辑注意到我隐藏了结束标签,有时候它是没有他们的工作,但最好的做法是总是使用它们,因为它们可能会导致有趣的错误(在这种情况下,生成的代码中缺少一个括号)。

edit noticed that I hade missed the closing tags, sometimes it works without them, but it's good practice to always use them as they could cause interesting errors (in this case a missing bracket on the generated code).

这篇关于如何调用XTemplate(itemTpl)中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆