2012-07-16 56 views
0

我想聲明一個全局變量,該變量從DataStore獲取值並在其警報窗口中顯示其值。來自JSON 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); 

但是,只有我拿到的就是警告窗口,上面寫着:正在執行正在回調之前,您storeTeacher.load回調執行您的通話alert(TeacherName);未定義

回答

0

如果你把alert(TeacherName)在回調函數就會正確填寫您的TeacherName和警報正確顯示:

storeTeacher.load({ 
    scope: this,   
    callback: function (records, operation, success) {      
     TeacherName = storeTeacher.getAt(0).get('NameT'); 
     alert(TeacherName); 
} }); 

這是正常現象。

+0

謝謝。現在一切正常 – Goran 2012-07-16 13:09:49