【问题标题】:ExtJS 4.2.1 XTemplate and subtemplates (statics)ExtJS 4.2.1 XTemplate 和子模板(静态)
【发布时间】:2013-06-21 16:00:48
【问题描述】:

我有一个自定义 Ext.Component 和一个视图 XTemplates。我确实需要一些在我的控制器视图之外的模板。

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

类似这样的:

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()   
});

我也不知道如何应用属于视图成员的子模板。我宣布他们是全球性的,知道我真的不喜欢做什么。

请!

【问题讨论】:

  • 我看不出您提出的解决方案有什么问题...除非您应该使用app.view.ApplicationHeader.mainIconTpl,而不是可能未定义的view.mainIconTpl。你遇到了什么样的失败?
  • 问题是我没有在这个 XTemplate 函数中获得我的视图上下文...:/
  • 如果它们在同一个视图中,我不知道如何在我的 XTemplate 中获取静态模板.. 我不能使用 this.self... 因为这已经是 XTemplate
  • app 也没有定义..

标签: javascript templates extjs static extjs4.2


【解决方案1】:

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

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

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

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();
        }
    });

});

【讨论】:

    【解决方案2】:

    根据链接,您应该可以将其直接放在您的 XTemplate 中。无需静态

    {[ MyApp.tpls.someOtherTpl.apply(values) ]}
    

    Multiple templates in Nested List

    您也可以尝试将所有这些 XTemplate 放入 initComponent 中,因为在初始组件渲染后您没有为 XTemplate 注入任何值。 apply() 将只返回一个 HTML 片段,该片段应该能够附加到 XTemplate 中的任何位置。

    如果您尝试将逻辑或条件 tpl 运算符,即 ... 放在任何子 XTemplates 中,那么这是另一个问题,所以这完全取决于你正在努力完成。

    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();
        }
    });
    

    【讨论】:

    • 我对 statcs 的想法是因为我只需要在组件的控制器中重新渲染一个子模板。有没有更好的解决方案?我确实有条件 tpl 运营商,但我不认为他们是问题..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    相关资源
    最近更新 更多