我想声明一个从DataStore获取值并在警告窗口中显示其值的全局变量。
var params = 1;
var storeTeacher = new Ext.data.JsonStore({
id: 'IDstoreTeacher',
url: 'school.php',
method: 'POST',
baseParams:{task: "LABEL",
parametar: params},
root: 'rows',
fields:
[{name: 'NameT', type: 'string', mapping: 'Teacher_name'}],
autoLoad: true
});
var TeacherName;
storeTeacher.load({
scope: this,
callback: function (records, operation, success) {
TeacherName = storeTeacher.getAt(0).get('NameT');
}
});
alert(TeacherName);
但是,我得到的只是提示窗口:未定义
答案 0 :(得分:0)
在执行alert(TeacherName);
回调的回叫之前,您的storeTeacher.load
来电正在执行。
如果您将alert(TeacherName)
放入回调函数中,它将正确填充您的TeacherName并在警报中正确显示:
storeTeacher.load({
scope: this,
callback: function (records, operation, success) {
TeacherName = storeTeacher.getAt(0).get('NameT');
alert(TeacherName);
} });
此行为是预期的。