14

我想对一些将输出到视图的文本使用 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(''),

但当然,第 10 行的串联是非法的。

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

4

3 回答 3

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

你可以在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,

...

Senchafiddle 示例

这应该和下面的代码一样正常工作(只需插入上面 XTemplate 中的代码)。

itemTpl: new XTemplate(...),

Senchafiddle 示例

希望这能解决问题!

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

于 2012-09-25T21:25:49.660 回答
1

注意:下面的示例不能按预期工作!查看zelexir的答案以获得澄清!

您可以使用成员函数

itemTpl: [
    ...
    '<tpl switch="post_type">',
    '<tpl case="new_user">',
        '<p>{post_text_teaser}</p>',
        '<p>{timestamp}</p>',
    '<tpl default>',
        '<p>{[this.doAction(post_text_teaser)]}</p>',
    ...,
    {
        // XTemplate configuration:
        disableFormats: true,
        // member functions:
        doAction: function(name){
           return Ext.String.ellipsis(name + "\", 4);
        }
    }
]
于 2012-09-25T18:50:46.517 回答
0

您可以在模板中使用函数

itemTpl: [
    ...
    '<tpl switch="post_type">',
    '<tpl case="new_user">',
        '<p>{post_text_teaser}</p>',
        '<p>{timestamp}</p>',
    '<tpl default>',
        '<p>{[this.concatenate(values.post_text_teaser)]}</p>',
    ...,
    {
        concatenate: function(teaser) {
               return Ext.String.ellipsis( + teaser + \, 4);
        }
    }
]
于 2012-09-25T18:52:24.840 回答