如何将TreeStore与';自动';映射?

如何将TreeStore与';自动';映射?,tree,sencha-touch,Tree,Sencha Touch,我有一个这样的模型: Ext.regModel('TreeItem', { fields: [ { name: 'ItemId', type: 'int' }, { name: 'ItemType', type: 'string' }, { name: 'Article', type: 'auto' }, { name: 'Container', type: 'auto' },

我有一个这样的模型:

Ext.regModel('TreeItem', {
    fields: [
             { name: 'ItemId', type: 'int' },
             { name: 'ItemType', type: 'string' },
             { name: 'Article', type: 'auto' },
             { name: 'Container', type: 'auto' },
             { name: 'Category', type: 'auto'}]
});
getItemTextTpl: function (recordnode)
            {
                var template = '<div class="{ItemType}-icon"></div>';
                if (recordnode.firstChild.attributes.record.data["ItemType"] == 'Article')
                {
                    template += recordnode.firstChild.attributes.record.data['Article']["ArticleName"];
                }
                if (recordnode.firstChild.attributes.record.data["ItemType"] == 'Container')
                {
                    template += recordnode.firstChild.attributes.record.data['Container']["ContainerName"];
                }
                if (recordnode.firstChild.attributes.record.data["ItemType"] == 'Category')
                {
                    template += recordnode.firstChild.attributes.record.data['Category']["CategoryName"];
                }
                return template;   
            }
ItemType指示是否应将特定项呈现为项目、容器或类别。每个项目、容器和类别对象都有关联的ArticleName、ContainerName和CategoryName

我想根据记录的ItemType在NestedList中呈现这些名称。所以我重写了getItemTextTpl,如下所示:

Ext.regModel('TreeItem', {
    fields: [
             { name: 'ItemId', type: 'int' },
             { name: 'ItemType', type: 'string' },
             { name: 'Article', type: 'auto' },
             { name: 'Container', type: 'auto' },
             { name: 'Category', type: 'auto'}]
});
getItemTextTpl: function (recordnode)
            {
                var template = '<div class="{ItemType}-icon"></div>';
                if (recordnode.firstChild.attributes.record.data["ItemType"] == 'Article')
                {
                    template += recordnode.firstChild.attributes.record.data['Article']["ArticleName"];
                }
                if (recordnode.firstChild.attributes.record.data["ItemType"] == 'Container')
                {
                    template += recordnode.firstChild.attributes.record.data['Container']["ContainerName"];
                }
                if (recordnode.firstChild.attributes.record.data["ItemType"] == 'Category')
                {
                    template += recordnode.firstChild.attributes.record.data['Category']["CategoryName"];
                }
                return template;   
            }
getItemTextTpl:函数(recordnode)
{
var模板=“”;
if(recordnode.firstChild.attributes.record.data[“ItemType”]=“Article”)
{
template+=recordnode.firstChild.attributes.record.data['Article'][“ArticleName”];
}
if(recordnode.firstChild.attributes.record.data[“ItemType”]=“容器”)
{
template+=recordnode.firstChild.attributes.record.data['Container']['ContainerName];
}
if(recordnode.firstChild.attributes.record.data[“ItemType”]=“类别”)
{
template+=recordnode.firstChild.attributes.record.data['Category'][“CategoryName”];
}
返回模板;
}
但是,对于树的每个级别,似乎只调用一次getItemTextTpl,因此无法呈现每个列表项的信息


有人对如何做到这一点有什么建议吗?提前感谢。

您应该将条件逻辑从函数移到模板中。下面的示例演示了如何执行此操作(尽管您可能需要修改它才能使其正常工作):

getItemTextTpl:函数(recordnode)
{
变量模板=“”+
'' +
“{ArticleName}”+
'' +
'' +
“{ContainerName}”+
'' +
'' +
“{CategoryName}”+
'' +
'';
返回模板;
}

我创建了一个使用这种技术的。同时也在演示它是如何建造的。您可能还想查看我关于Xtemplates主题的两部分系列屏幕广播(和)

这无法完成。我将对象泛化,并使用常规映射。您不能对模板使用自动映射。

是的,这是我应该做的,但是每个名称都在到文章、容器和类别对象的自动映射中,我不希望将它们移出,因为这些对象中有很多其他信息,我希望用它们来提供非常丰富的显示。(上面的代码只是一个示例。)是否可以访问模板中自动对象的属性?我认为应该可以将javascript条件转换为
格式,但必须转义某些字符会使阅读变得难看和不愉快。在这种情况下,我通常建议将内联条件从
标记移动到成员函数中。但遗憾的是,我不知道在这种上下文中附加成员函数的任何方法,因为
getItemTextTpl
应该返回字符串对象,而不是xTemplate。