Extjs 什么';Ext.define必须设置uses属性的情况是什么?
我已经搜索并阅读了这本书。它说:Extjs 什么';Ext.define必须设置uses属性的情况是什么?,extjs,extjs4,extjs4.1,extjs4.2,Extjs,Extjs4,Extjs4.1,Extjs4.2,我已经搜索并阅读了这本书。它说: Ext.define('Mother', { uses: ['Child'], giveBirth: function() { // This code might, or might not work: // return new Child(); // Instead use Ext.create() to load the class at the spot if not loaded al
Ext.define('Mother', {
uses: ['Child'],
giveBirth: function() {
// This code might, or might not work:
// return new Child();
// Instead use Ext.create() to load the class at the spot if not loaded already:
return Ext.create('Child'); // will find and load the Child.js file
}
});
但我尝试:
Ext.define('Mother', {
giveBirth: function() {
return Ext.create('Child'); // will find and load the Child.js file, too
}
});
设置uses属性时有什么区别?
谢谢 属性是与该类一起加载的可选类的列表。在创建此类之前,不一定要加载它们,但在调用
Ext.onReady
侦听器之前,它们保证是可用的
Uses
是指在实例化该类之前,需要定义那些类['Child']
。onReady
函数将延迟触发,直到定义了requires
类,但如果uses
类未定义,则将触发。uses
属性是要与该类一起加载的可选类的列表。在创建此类之前,不一定要加载它们,但在调用Ext.onReady
侦听器之前,它们保证是可用的
Uses
是指在实例化该类之前,需要定义那些类['Child']
。onReady
函数将推迟到定义了requires
类后才触发,但如果uses
类未定义,则会触发。有两种方法可以声明依赖项,建议将它们放在requires
属性中:
Ext.define('Foo', {
requires: ['Bar'],
getBar: function() {
// Bar class is guaranteed to be loaded
return new Bar();
}
});
事实上,使用
只有一个目的:打破循环依赖关系
Ext.define('Foo', {
requires: ['Bar'],
...
});
Ext.define('Bar', {
// Can't use requires here, as this would create a deadlock
// OTOH we should let the Loader know that Bar depends
// on Foo, so that Foo would be loaded too, eventually
uses: ['Foo'],
...
});
从技术上讲,要求
将保证在创建依赖于依赖项的类之前加载依赖项uses
仅保证在触发onready
事件之前(即当所有内容都已加载时)加载依赖项
TL;DR您根本不应该使用
uses
。请改用requires
。有两种方法可以声明依赖项,建议将它们放在requires
属性中:
Ext.define('Foo', {
requires: ['Bar'],
getBar: function() {
// Bar class is guaranteed to be loaded
return new Bar();
}
});
事实上,使用
只有一个目的:打破循环依赖关系
Ext.define('Foo', {
requires: ['Bar'],
...
});
Ext.define('Bar', {
// Can't use requires here, as this would create a deadlock
// OTOH we should let the Loader know that Bar depends
// on Foo, so that Foo would be loaded too, eventually
uses: ['Foo'],
...
});
从技术上讲,要求
将保证在创建依赖于依赖项的类之前加载依赖项uses
仅保证在触发onready
事件之前(即当所有内容都已加载时)加载依赖项
TL;DR您根本不应该使用
uses
。改用requires
。这是为了避免循环依赖
Ext.define('Foo', {
requires: ['Bar'],
...
});
Ext.define('Bar', {
// Can't use requires here, as this would create a deadlock
// OTOH we should let the Loader know that Bar depends
// on Foo, so that Foo would be loaded too, eventually
uses: ['Foo'],
...
});
考虑以下示例:
src/Foo.js
Ext.define('Test.Bar', {
requires: 'Test.Foo'
}, function() {
console.log('Loaded Bar');
});
Ext.define('Test.Foo', {
requires: 'Test.Bar'
}, function() {
console.log('Loaded Foo');
});
Ext.Loader.setConfig({
enabled: true
,paths: {
Test: 'src'
}
});
Ext.require('Test.Foo');
Ext.onReady(function() {
alert('Everything\'s loaded!');
});
src/Bar.js
Ext.define('Test.Bar', {
requires: 'Test.Foo'
}, function() {
console.log('Loaded Bar');
});
Ext.define('Test.Foo', {
requires: 'Test.Bar'
}, function() {
console.log('Loaded Foo');
});
Ext.Loader.setConfig({
enabled: true
,paths: {
Test: 'src'
}
});
Ext.require('Test.Foo');
Ext.onReady(function() {
alert('Everything\'s loaded!');
});
app.js
Ext.define('Test.Bar', {
requires: 'Test.Foo'
}, function() {
console.log('Loaded Bar');
});
Ext.define('Test.Foo', {
requires: 'Test.Bar'
}, function() {
console.log('Loaded Foo');
});
Ext.Loader.setConfig({
enabled: true
,paths: {
Test: 'src'
}
});
Ext.require('Test.Foo');
Ext.onReady(function() {
alert('Everything\'s loaded!');
});
警报永远不会发出。如果您尝试使用开发版本(例如ext-all-dev.js
),加载程序将告诉您原因:
> Uncaught Error: Deadlock detected while loading dependencies! 'Test.Foo' and 'Test.Bar' mutually require each other. Path: Test.Foo -> Test.Bar -> Test.Foo
将两个类中的任何一个类中的需要
替换为使用
,并解决问题,发出警报
要解决问题的最后一部分,应避免使用Ext.create
完全。。。部分出于性能原因,但特别是在开发模式下,当启用动态加载程序时,如果使用它,您不会注意到缺少requires
。当你试图编译你的应用程序时,这可能会花费你一些时间来找到所有缺少的一个。。。虽然这会导致崩溃,但您不能错过:
new My.Class();
这是为了避免循环依赖
Ext.define('Foo', {
requires: ['Bar'],
...
});
Ext.define('Bar', {
// Can't use requires here, as this would create a deadlock
// OTOH we should let the Loader know that Bar depends
// on Foo, so that Foo would be loaded too, eventually
uses: ['Foo'],
...
});
考虑以下示例:
src/Foo.js
Ext.define('Test.Bar', {
requires: 'Test.Foo'
}, function() {
console.log('Loaded Bar');
});
Ext.define('Test.Foo', {
requires: 'Test.Bar'
}, function() {
console.log('Loaded Foo');
});
Ext.Loader.setConfig({
enabled: true
,paths: {
Test: 'src'
}
});
Ext.require('Test.Foo');
Ext.onReady(function() {
alert('Everything\'s loaded!');
});
src/Bar.js
Ext.define('Test.Bar', {
requires: 'Test.Foo'
}, function() {
console.log('Loaded Bar');
});
Ext.define('Test.Foo', {
requires: 'Test.Bar'
}, function() {
console.log('Loaded Foo');
});
Ext.Loader.setConfig({
enabled: true
,paths: {
Test: 'src'
}
});
Ext.require('Test.Foo');
Ext.onReady(function() {
alert('Everything\'s loaded!');
});
app.js
Ext.define('Test.Bar', {
requires: 'Test.Foo'
}, function() {
console.log('Loaded Bar');
});
Ext.define('Test.Foo', {
requires: 'Test.Bar'
}, function() {
console.log('Loaded Foo');
});
Ext.Loader.setConfig({
enabled: true
,paths: {
Test: 'src'
}
});
Ext.require('Test.Foo');
Ext.onReady(function() {
alert('Everything\'s loaded!');
});
警报永远不会发出。如果您尝试使用开发版本(例如ext-all-dev.js
),加载程序将告诉您原因:
> Uncaught Error: Deadlock detected while loading dependencies! 'Test.Foo' and 'Test.Bar' mutually require each other. Path: Test.Foo -> Test.Bar -> Test.Foo
将两个类中的任何一个类中的需要
替换为使用
,并解决问题,发出警报
要解决问题的最后一部分,应避免使用Ext.create
完全。。。部分出于性能原因,但特别是在开发模式下,当启用动态加载程序时,如果使用它,您不会注意到缺少requires
。当你试图编译你的应用程序时,这可能会花费你一些时间来找到所有缺少的一个。。。虽然这会导致崩溃,但您不能错过:
new My.Class();