我得到了一个带有视图XTemplates的自定义Ext.Component。我也需要在我的控制器中的视图之外的一些模板。
是否可以在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()
});我也不知道如何应用子模板,这是视图的成员。我宣布他们是全球权利,知道我真的不喜欢这样做。
请!
发布于 2013-06-24 16:39:55
您的代码无法正常工作,因为在调用类定义(即define方法)之前,主模板的apply方法被称为。
您可以在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();
}
});
});发布于 2013-06-22 01:18:21
根据链接,您应该能够将其直接放入您的XTemplate中。不需要静态
{[ MyApp.tpls.someOtherTpl.apply(values) ]}Multiple templates in Nested List
您也可以尝试将所有这些XTemplates放在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();
}
});https://stackoverflow.com/questions/17239579
复制相似问题