ExtJS:如何在 initComponent 方法中等待 Promise 响应?
ExtJS: How to waiting for Promise response in initComponent method?
我已经将 initComponent
方法实现到 Container
class 并通过 Promise
方法加载 items
配置。
debugging
期间的事情首先转到 callParent
行然后执行转到 Promise
方法。我必须重新加载浏览器才能获取 Promise
回应。
我尝试了 initComponent
中的 then()
方法,但出现错误。我怎样才能实现这种用法?
- 容器 class 与 initComponent
;
Ext.define('MyApp.view.Bar', {
extend: 'Ext.container.Container',
xtype: 'barview',
requires: [],
controller: 'barctrl',
viewModel: {
type: 'barvm'
},
initComponent: function () {
let me = this;
let fooInfo = MyApp.FooInfoClass.getFooInfo(); //There is a debugger in FooInfoClass but miss this one
if (fooInfo) {
if (fooInfo.self) {
me.items = [
{
xtype: 'componentA'
}
];
} else if (fooInfo.reference) {
me.items = [
{
xtype: 'componentB'
}
];
} else {
me.items = [
{
xtype: 'componentC'
}
];
}
}
debugger //Firstly comes to this one then goes to Promise
me.callParent(arguments);
}
});
-承诺class和方法;
Ext.define('MyApp.FooInfoClass', {
requires: [],
singleton: true,
endpoint: MyApp.Url(),
getFooInfo: function () {
let me = this;
let responseInfo = localStorage.getItem('foo-info');
return JSON.parse(responseInfo);
},
setFooInfo: function (foo) {
let me = this;
me.foo = JSON.stringify(foo);
localStorage.setItem(me.fooInfo, me.foo);
},
fooInfo: function () {
let me = this;
return new Ext.Promise(function (resolve, reject) {
Ext.Ajax.request({
url: me.endpoint + '/info',
success: function(response, opts) {
let obj = Ext.decode(response.responseText);
let data = obj.data[0];
debugger //Secondly comes to here!
me.foo = data;
me.setFooInfo(me.foo);
resolve(me.foo);
},
failure: function(response, opts) {
console.log('server-side failure with status code ' + response);
reject(response);
}
});
});
}
});
您不应推迟完成 initComponent
函数,直到您拥有所需的数据。要么先获取数据,然后在所有必需信息一起实例化组件(在 Promise.then()
中执行 Ext.create()
),要么让组件获取所需信息并添加子组件 在运行时 基于这些信息。在 initComponent
上调用 callParent
之前,您真正 知道的事情很少。
如果我是你,而不是使用 me.items =
,我会使用 me.add()
以便在运行时添加子组件,在初始化之后。
我为你做了一个简单的fiddle:https://fiddle.sencha.com/#view/editor&fiddle/2k9r
我已经将 initComponent
方法实现到 Container
class 并通过 Promise
方法加载 items
配置。
debugging
期间的事情首先转到 callParent
行然后执行转到 Promise
方法。我必须重新加载浏览器才能获取 Promise
回应。
我尝试了 initComponent
中的 then()
方法,但出现错误。我怎样才能实现这种用法?
- 容器 class 与 initComponent
;
Ext.define('MyApp.view.Bar', {
extend: 'Ext.container.Container',
xtype: 'barview',
requires: [],
controller: 'barctrl',
viewModel: {
type: 'barvm'
},
initComponent: function () {
let me = this;
let fooInfo = MyApp.FooInfoClass.getFooInfo(); //There is a debugger in FooInfoClass but miss this one
if (fooInfo) {
if (fooInfo.self) {
me.items = [
{
xtype: 'componentA'
}
];
} else if (fooInfo.reference) {
me.items = [
{
xtype: 'componentB'
}
];
} else {
me.items = [
{
xtype: 'componentC'
}
];
}
}
debugger //Firstly comes to this one then goes to Promise
me.callParent(arguments);
}
});
-承诺class和方法;
Ext.define('MyApp.FooInfoClass', {
requires: [],
singleton: true,
endpoint: MyApp.Url(),
getFooInfo: function () {
let me = this;
let responseInfo = localStorage.getItem('foo-info');
return JSON.parse(responseInfo);
},
setFooInfo: function (foo) {
let me = this;
me.foo = JSON.stringify(foo);
localStorage.setItem(me.fooInfo, me.foo);
},
fooInfo: function () {
let me = this;
return new Ext.Promise(function (resolve, reject) {
Ext.Ajax.request({
url: me.endpoint + '/info',
success: function(response, opts) {
let obj = Ext.decode(response.responseText);
let data = obj.data[0];
debugger //Secondly comes to here!
me.foo = data;
me.setFooInfo(me.foo);
resolve(me.foo);
},
failure: function(response, opts) {
console.log('server-side failure with status code ' + response);
reject(response);
}
});
});
}
});
您不应推迟完成 initComponent
函数,直到您拥有所需的数据。要么先获取数据,然后在所有必需信息一起实例化组件(在 Promise.then()
中执行 Ext.create()
),要么让组件获取所需信息并添加子组件 在运行时 基于这些信息。在 initComponent
上调用 callParent
之前,您真正 知道的事情很少。
如果我是你,而不是使用 me.items =
,我会使用 me.add()
以便在运行时添加子组件,在初始化之后。
我为你做了一个简单的fiddle:https://fiddle.sencha.com/#view/editor&fiddle/2k9r