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

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

问题描述

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

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?

推荐答案

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

This should solve your problem:

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

模板成员函数的事情是,据我所知,您不能以常规方式直接在 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 注意到我错过了结束标签,有时没有它们也能工作,但最好总是使用它们,因为它们可能会导致有趣的错误(在这种情况下,生成的括号中缺少括号代码).

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天全站免登陆