来自JSON DataStore的全局变量

时间:2012-07-16 12:39:24

标签: extjs

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

但是,我得到的只是提示窗口:未定义

1 个答案:

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

此行为是预期的。