extjs的alias和xtype比较

 今天在帮一个兄弟检查一段自定义控件的代码时觉得他对xtype以及alias的使用和ExtJS各示例代码的使用有较多的不一致,而我自己也不是很清楚使用这两个属性时的最佳方法。因此在回家后整理出了这样一篇文档。一方面用来标准化我们自己的代码,另一方面也共享给大家,毕竟对这两个属性进行详细讨论的资料几乎没有。

 

xtype

  首先来看看xtype的定义。在ExtJS的官方文档中是这样对它进行定义的:

  This property provides a shorter alternative to creating objects than using a full class name. Using xtype is the most common way to define component instances, especially in a container.

  也就是说,在定义一个类的时候,如果我们指定了它的xtype,那么我们就可以通过这个xtype,而不是类型的全名来创建这些类型。例如对于下面的布局声明:

1 items: [
2     Ext.create('Ext.form.field.Text', {
3         fieldLabel: 'Foo'
4     }),
5     ……
6 ]

  其与以下使用xtype声明的布局是等同的:

复制代码
1 items: [
2     {
3         xtype: 'textfield',
4         fieldLabel: 'Foo'
5     },
6     ……..
7 ]
复制代码

  可以看到,在使用xtype的时候,我们可以不再标明类型的全名,进而减少了在使用它们时出错的可能,降低了维护的成本。

  而在定义一个类型的时候,我们可以指定该类型所具有的xtype:

1 Ext.define('MyApp.PressMeButton', {
2     extend: 'Ext.button.Button',
3     xtype: 'pressmebutton',
4     text: 'Press Me'
5 });

 

alias

  而在文档中,alias的定义则如下所示:

  List of short aliases for class names. An alias consists of a namespace and a name concatenated by a period as <namespace>.<name>

namespace - The namespace describes what kind of alias this is and must be all lowercase.

name - The name of the alias which allows the lazy-instantiation via the alias. The name shouldn't contain any periods.

  在一个类型定义中,我们可以指定它所使用的alias:

1 Ext.define('MyApp.CoolPanel', {
2     extend: 'Ext.panel.Panel',
3     alias: ['widget.coolpanel'],
4     title: 'Yeah!'
5 });

  而对这个类型的使用也非常简单,在xtype中标示该alias即可:

1 Ext.widget('panel', {
2     items: [
3         {xtype: 'coolpanel', html: 'Foo'},
4         {xtype: 'coolpanel', html: 'Bar'}
5     ]
6 });

 

xtype vs. alias

  可以看到,xtype和alias有点像,是吧?那么它们两个有什么区别,什么时候用xtype,什么时候用alias呢?

  上面的示例也展示了一个比较有趣的事情,那就是通过alias所定义的别名“coolpanel”可以直接通过xtype引用。也就是说,xtype和alias实际上可以在一定程度上通用的。

  最终我在ClassManager类的源码中找到了一系列证据。简单地说,xtype是存在于widget命名空间下的alias。如果为一个新的UI组成声明了它的xtype,那么就等于在widget命名空间下为其声明了一个alias。例如我们也可以通过下面的代码定义刚刚看到的CoolPanel类:

1 Ext.define('MyApp.CoolPanel', {
2     extend: 'Ext.panel.Panel',
3     xtype: ‘coolpanel’,
4     title: 'Yeah!'
5 });

  总的来说,为一个UI组成指定一个xtype实际上就等于为其指定一个在widget命名空间下的alias。但是alias所能覆盖的类型范围要比xtype广得多。一个alias不仅仅可以用来声明命名空间为widget的各个类型,更可以在plugin,proxy,layout等众多命名空间下声明类型。

  而在Sencha论坛中,一位开发人员也解释了为什么在alias已经存在的情况下定义一个额外的xtype。这仅仅是为了提高性能。在ExtJS的内部实现中常常需要将alias中的命名空间剥离才能得到所需要创建的widget类型。在xtype的帮助下,ExtJS可以摆脱掉耗时的字符串分析工作,从而提高性能。

  因此在定义一个自定义widget的时候,我们应当使用xtype,而在定义其它组成时,我们就不得不使用alias了。由于所有的widget使用同一个命名空间,因此我们需要在为自定义widget指定xtype时标示一个前缀,例如在项目egoods中定义的一个自定义button的xtype就应为egoods-button。


原文地址:

https://www.cnblogs.com/loveis715/p/4423502.html


以下是一个使用ExtJS 6实现条形统计图和折线图切换的简单案例: 1.首先,我们需要定义一个包含条形统计图和折线图的Panel: ``` Ext.define('MyApp.view.MyChartPanel', { extend: 'Ext.panel.Panel', xtype: 'mychartpanel', requires: [ 'Ext.chart.CartesianChart', 'Ext.chart.axis.Category', 'Ext.chart.axis.Numeric', 'Ext.chart.series.Bar', 'Ext.chart.series.Line' ], layout: 'fit', items: [{ xtype: 'cartesian', reference: 'chart', bind: '{chartData}', insetPadding: { top: 20, bottom: 20, left: 20, right: 40 }, axes: [{ type: 'category', fields: ['name'], position: 'bottom', label: { rotate: { degrees: -45 } } }, { type: 'numeric', fields: ['value'], position: 'left', grid: true }], series: [{ type: 'bar', xField: 'name', yField: 'value', style: { opacity: 0.80 }, highlight: { fillStyle: 'yellow' }, tooltip: { trackMouse: true, renderer: function (tooltip, record, item) { tooltip.setHtml(record.get('name') + ': ' + record.get('value')); } } }, { type: 'line', xField: 'name', yField: 'value', marker: { type: 'circle', radius: 4, lineWidth: 2, fillStyle: 'white' }, style: { lineWidth: 2 }, tooltip: { trackMouse: true, renderer: function (tooltip, record, item) { tooltip.setHtml(record.get('name') + ': ' + record.get('value')); } } }] }], tbar: [{ text: 'Switch to Bar Chart', handler: 'onSwitchToBarChart' }, { text: 'Switch to Line Chart', handler: 'onSwitchToLineChart' }] }); ``` 2.在控制器中,我们需要定义两个事件处理程序,用于切换到条形统计图和折线图: ``` Ext.define('MyApp.view.MyChartPanelController', { extend: 'Ext.app.ViewController', alias: 'controller.mychartpanel', onSwitchToBarChart: function () { var chart = this.lookupReference('chart'); chart.getSeries()[0].setType('bar'); chart.getSeries()[1].hide(); }, onSwitchToLineChart: function () { var chart = this.lookupReference('chart'); chart.getSeries()[0].setType('line'); chart.getSeries()[1].show(); } }); ``` 在这里,我们使用getSeries()方法获取图表的两个系列,然后使用setType()方法将第一个系列切换为条形统计图或折线图,使用show()和hide()方法显示或隐藏第二个系列。 3.最后,我们需要定义一个包含示例数据的ViewModel: ``` Ext.define('MyApp.view.MyChartPanelModel', { extend: 'Ext.app.ViewModel', alias: 'viewmodel.mychartpanel', data: { chartData: [{ name: 'Item 1', value: 10 }, { name: 'Item 2', value: 7 }, { name: 'Item 3', value: 5 }, { name: 'Item 4', value: 2 }, { name: 'Item 5', value: 27 }] } }); ``` 4.最后,在应用程序中使用MyChartPanel: ``` Ext.application({ name: 'MyApp', requires: ['MyApp.view.MyChartPanel'], launch: function () { Ext.create('Ext.container.Viewport', { layout: 'fit', items: [{ xtype: 'mychartpanel' }] }); } }); ``` 现在,我们可以在条形统计图和折线图之间切换,如下所示: ![chart-switch.png](https://img-blog.csdn.net/20180816102650168?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BhY2thZ2Vz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/85)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值