问题
I was wondering: Why is the alias of new widgets we define always defined as "widget.myxtype" - what is the significance of the "widget" in that?
回答1:
I think it's a kind of namespace : you can also define aliases for proxies, readers and writers, which respectively need to be prefixed with 'proxy.', 'reader.' and 'writer.' :
Ext.define('App.proxy.MyProxy', {
extend: 'Ext.data.proxy.Rest',
alias: ['proxy.my_proxy'],
Ext.define('App.reader.MyReader', {
extend: 'Ext.data.reader.Json',
alias: ['reader.my_reader'],
Ext.define('App.reader.MyWriter', {
extend: 'Ext.data.writer.Json',
alias: ['writer.my_writer'],
Then you can reference them with :
proxy: {
type: 'my_proxy',
url: '/data/library_store.json',
reader: {
type: 'my_reader',
...
},
writer: 'my_writer'
回答2:
Alias property was newly introduced in ExtJs4 and is a substitute for "xtype" in previous versions.The "widget" specified in alias do have two significance that I can think of:
- Make use of the shorthand
- Code readability and understanding
You can make use of the shorthand to create a widget. For example, if you have a widget named widget.myview, you can create an instance of it as follows:
var view = Ext.widget('myview');
If you don't prefix the "widget" in alias, you can instantiate only through:
var view = Ext.create('xyz.myview');
Also, the create method can be used to create an instance of any class that has been defined using Ext.define().
when you prefix "widget", you are sure they will deal with UI. This helps in better understanding and reading of the code (when you see Ext.widget('abc'), you know its a window,button or some UI component).
Update: alias property is used by other classes that are not UI component. But, you will see "widget" for UI components only hence denoting it as an view or UI (that is why they have the shorthand for widget alone). Other ExtJS classes also have alias. For example, all the Proxy classes will have "proxy" prefix. But for now, the only significance for the "proxy" prefix is readability.
来源:https://stackoverflow.com/questions/7376969/why-is-the-alias-of-views-defined-as-widget-myxtype