首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ExtJS 4.2.1 XTemplate和子模板(静态)

ExtJS 4.2.1 XTemplate和子模板(静态)
EN

Stack Overflow用户
提问于 2013-06-22 00:00:48
回答 2查看 1.7K关注 0票数 1

我得到了一个带有视图XTemplates的自定义Ext.Component。我也需要在我的控制器中的视图之外的一些模板。

是否可以在XTemplate的函数中引用静态成员。还是有别的更好的方法?

如下所示:

代码语言:javascript
复制
Ext.define('app.view.ApplicationHeader', {
    extend: 'Ext.Component',
    name: 'app-header',
    xtype: 'app-header',

    height: 67,
    margin: 0,

    statics: {
        mainIconTpl: new Ext.XTemplate('someTemplate'),

        navigationItemsTpl: new Ext.XTemplate( 'anotherTemplate'),

        userInfoTpl: new Ext.XTemplate('userTemplate')
    },

    html: new Ext.XTemplate('... {[ this.renderMainIcons() ]} {[ this.renderUserInfo() ]} ...',
            '... {[ this.renderNavigationBarItems() ]} ...',
            {
                me: this,
                renderMainIcons: function () {
                    return view.static.mainIconTpl.apply(MR.Sitemap.Items);
                },
                renderUserInfo: function () {
                    return view.static.userInfoTpl.apply();
                },
                renderNavigationBarItems: function () {
                    return view.static.navigationItemsTpl.apply();
                }
            }).apply()   
});

我也不知道如何应用子模板,这是视图的成员。我宣布他们是全球权利,知道我真的不喜欢这样做。

请!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-24 16:39:55

您的代码无法正常工作,因为在调用类定义(即define方法)之前,主模板的apply方法被称为

您可以在post-create函数中创建使用类的其他静态成员的静态模板(参见define method的最后一个参数)。

然后,为了使模板可用,我将覆盖initComponent方法并在其中设置html属性。

代码语言:javascript
复制
Ext.define('app.view.ApplicationHeader', {
    extend: 'Ext.Component',
    name: 'app-header',
    xtype: 'app-header',

    height: 67,
    margin: 0,

    statics: {
        mainIconTpl: new Ext.XTemplate('someTemplate'),
        navigationItemsTpl: new Ext.XTemplate('anotherTemplate'),
        userInfoTpl: new Ext.XTemplate('userTemplate')
    },

    initComponent: function() {

        // Here, your statics are available, and you're in the scope of your
        // class *instance*
        this.html = this.self.viewTemplate.apply();

        this.callParent(arguments);
    }

}, function() {

    // In the post create function, this is the class constructor
    // (i.e. app.view.ApplicationHeader)
    var cls = this;

    // In fact, you could also create your sub templates here if you prefer
    // e.g.
    // cls.useInfoTpl = new Ext.XTemplate('userTemplate')

    // So, viewTemplate will be a static property of the class
    cls.viewTemplate = new Ext.XTemplate('... {[ this.renderMainIcons() ]} {[ this.renderUserInfo() ]} ...',
        '... {[ this.renderNavigationBarItems() ]} ...', {
        renderMainIcons: function() {
            return cls.mainIconTpl.apply();
        },
        renderUserInfo: function() {
            return cls.userInfoTpl.apply();
        },
        renderNavigationBarItems: function() {
            return cls.navigationItemsTpl.apply();
        }
    });

});
票数 1
EN

Stack Overflow用户

发布于 2013-06-22 01:18:21

根据链接,您应该能够将其直接放入您的XTemplate中。不需要静态

代码语言:javascript
复制
{[ MyApp.tpls.someOtherTpl.apply(values) ]}

Multiple templates in Nested List

您也可以尝试将所有这些XTemplates放在initComponent中,因为在初始组件呈现之后,您不会为XTemplate注入任何值。apply()将只返回一个HTML片段,它应该能够被附加到XTemplate中的任何地方。

如果您试图将逻辑或条件tpl运算符...在任何子XTemplates中,这都是另一个问题,所以这完全取决于您试图实现的目标。

代码语言:javascript
复制
Ext.define('app.view.ApplicationHeader', {
    extend: 'Ext.Component',
    name: 'app-header',
    xtype: 'app-header',

    height: 67,
    margin: 0,

    initComponent: function() {
        var me = this,
        me.mainIconTpl = new Ext.XTemplate('someTemplate'),
        me.navigationItemsTpl = new Ext.XTemplate( 'anotherTemplate'),
        me.userInfoTpl = new Ext.XTemplate('userTemplate');

        me.tpl = new Ext.XTemplate(
            '...', me.mainIconTpl.apply(MR.Sitemap.Items), 
            '...', me.navigationItemsTpl.apply(someValues), 
            '...', me.userinfoTpl.apply(someValues), 
            '...'
        );

        Ext.apply(me, {
             html: me.tpl
        });

        me.callParent();
    }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17239579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档