更正下,这时:alert(mytore.getCount())值应该为1吧?(还是为2?因为,root是根结点,不能算个数吧?)
当然我本意是想要得到为2的,因为,里边有两个值。
怎么弄,我当然得到值为2呢?

解决方案 »

  1.   

    另外,如果我不用root,得到的值也是0,此时代码为:
    Ext.define('User', {
         extend: 'Ext.data.Model',
         fields: [
             {name: 'id', type: 'string'},
             {name: 'name',  type: 'string'}
         ]
     });
     
     var myStore = Ext.create('Ext.data.Store', {
         model: 'User',
         proxy: {
             type: 'ajax',
             url: '/users.json',
             reader: {
                 type: 'json'//,
                 //root: 'users'
             }
         },
         autoLoad: true
     });
    这时,URL得到的值为:[{"id":"1","name":"张三"},{"id":"2","name":"李四"}]
    alert(mytore.getCount())值依然为0
      

  2.   

    值还没到;function g(){
         var $a = userStore;
        
        
        
        }
        
        setTimeout(g,1000);
      

  3.   

    Ext.onReady(function() {
    Ext.define('User', {
         extend: 'Ext.data.Model',
         fields: [
             {name: 'id', type: 'string'},
             {name: 'name',  type: 'string'}
         ]
     });
     
     var myStore = Ext.create('Ext.data.Store', {
         model: 'User',
         proxy: {
             type: 'ajax',
             url: 'users.json',
             reader: {
                 type: 'json',
                 root: 'users'
             }
         },
         autoLoad: true
     });
     
     function g (){
     var $a = myStore;
     var $b = myStore.getCount();
     
     
     }
     setTimeout(g,1000);
     
          
        
        
    });
      

  4.   


    很神奇,我延迟了3秒,经多次刷新测试,发现有时候可以,有时候不可以,真是怪了,不知道是不是类库有BUG,还是我所在的测试环境有问题。要去下个最新版的再试下。
      

  5.   

    原因找到了,确实是延迟执行ajax的原因,导致myStore值还没得到,就执行了myStore.getCount();所以值为0,我延时了10秒后,基本上都能得到值,这也解释了为何我延时3秒,有时能得到值,有时得不到值的原因了。但是解决方法用这个延时貌似不科学啊,也不可靠,一个是延时时间这个并不是固定的,如果返回的数据多,则后台执行时间会更长。
    不知,有没办法,先返回数据后,即mystore得到值后,再往下执行呢,相当于阻塞模式,或同步什么的,水平有限,不知道怎么写,请大侠指教。
      

  6.   

    Ext.onReady(function() {




     
     Ext.define('User', {
         extend: 'Ext.data.Model',
         fields: [
             {name: 'id', type: 'string'},
             {name: 'name',  type: 'string'}
         ]
     });
     
     var myStore = Ext.create('Ext.data.Store', {
         model: 'User',
         proxy: {
             type: 'ajax',
             url: '/users.json',
             reader: {
                 type: 'json',
                 root: 'users'
             }
         },
         autoLoad: {
          callback : function(){
          var $a = myStore;
          var $b = myStore.getCount();
          alert($b);
          
          
          
         }}
     });
     
    });