因为我似乎没有在 Sencha Forum 上得到任何答案,我只是在这里重复我的问题:
我目前正在从 4.2.1 迁移到 5.1.0.107,但在理解某些内容时遇到问题:
Sencha 开发 evant状态here initConfig
只需要在不使用 Observable mixin 的类中手动调用构造函数即可。
docs 中 Observable mixin 的第一个示例否则通过手动调用它来表示。
按原样运行文档中的代码会导致异常(hasListeners 未定义),可以通过将 this.mixins.observable.constructor.call(this, config);
放入构造函数。
而且在我看来,initConfig
可以从文档的示例代码中删除,没有任何缺点(至少我无法重现)...
下面两段代码的唯一区别在于 Employee 构造函数......
Erroneous fiddle (从文档复制的代码)
Ext.define('Employee', {
mixins: ['Ext.mixin.Observable'],
config: {
fullName: ''
},
constructor: function(config) {
this.initConfig(config); // We need to initialize the config options when the class is instantiated
},
quitJob: function() {
this.fireEvent('quit');
}
});
var newEmployee = Ext.create('Employee', {
fullName: 'Ed Spencer',
listeners: {
quit: function() {
alert(this.getFullName() + " has quit!");
}
}
});
try {
newEmployee.quitJob(); // Throws exception since hasListener is undefined
} catch (exc) {
alert('Error occurred: ' + exc.message);
}
Working fiddle (删除了 initConfig 并正确初始化 mixin)
Ext.define('Employee', {
mixins: ['Ext.mixin.Observable'],
config: {
fullName: ''
},
constructor: function(config) {
// Make code work by removing call to initConfig and initializing the observable mixin
//this.initConfig(config); // We need to initialize the config options when the class is instantiated
this.mixins.observable.constructor.call(this, config);
},
quitJob: function() {
this.fireEvent('quit');
}
});
var newEmployee = Ext.create('Employee', {
fullName: 'Ed Spencer',
listeners: {
quit: function() {
alert(this.getFullName() + " has quit!");
}
}
});
try {
newEmployee.quitJob(); // Will log 'Ed Spencer has quit!'
} catch (exc) {
alert('Error occurred: ' + exc.message);
}
如果有人能够回答以下问题,我会非常高兴:
- 使用 Observable mixin 时是否需要在构造函数中手动调用
initConfig
- 我们需要手动调用 mixin 构造函数吗?
- 使用
Ext.mixin.Observable
和Ext.util.Observable
有什么区别
谢谢并致以诚挚的问候
最佳答案
这是 ExtJs 5.1 Upgrade Guide 的摘录-
"Unification of Ext.util.Observable and Ext.mixin.Observable APIs As mentioned in What’s New in Ext JS 5.1, Ext JS 5.1 still has two Observable classes (Ext.mixin.Observable, and Ext.util.Observable), but their API differences have been eliminated. There is only one exception: Ext.mixin.Observable calls initConfig in its constructor whereas Ext.util.Observable uses the legacy Ext.apply approach to copy config object properties onto the instance. We recommend that applications use Ext.mixin.Observable going forward, but we will continue to support Ext.util.Observable for the foreseeable future since many classes internal to the framework and in user code depend upon its behavior."
关于javascript - ExtJS 5 : initConfig method & Observable mixin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28805491/