0

我想声明一个从 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);

但是,我唯一得到的是警告窗口,上面写着:未定义

4

1 回答 1

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);   
} }); 

这种行为是预期的。

于 2012-07-16T12:44:23.463 回答