费解ext4中的Ext.data.Stroe,为何我用Ajax后台取到了值,但alert(mytore.getCount())老是为0?

网络科技 2013-12-22 10:20:43
先看代码:

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:/users.json取到的值为:
{users:[{"id":"1","name":"张三"},{"id":"2","name":"李四"}]} //在浏览器中输入URL,显示出的值

按理说,这时:alert(mytore.getCount())值应为2吧?但居然为0!实在看不出那里出问题了,请高人指教。
另外,我用的是ext4.1的,不知道版本有没BUG
...全文
118 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
网络科技 2013-12-22
  • 打赏
  • 举报
回复
引用 4 楼 u011461314 的回复:
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);
		 
		 

	    
	    
	    
	});
应该跟这个没关系吧,我加了延时几秒,也无效
zhjdg 2013-12-22
  • 打赏
  • 举报
回复
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);
		 
		 

	    
	    
	    
	});
zhjdg 2013-12-22
  • 打赏
  • 举报
回复
值还没到; function g(){ var $a = userStore; } setTimeout(g,1000);
网络科技 2013-12-22
  • 打赏
  • 举报
回复
另外,如果我不用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
网络科技 2013-12-22
  • 打赏
  • 举报
回复
更正下,这时:alert(mytore.getCount())值应该为1吧?(还是为2?因为,root是根结点,不能算个数吧?) 当然我本意是想要得到为2的,因为,里边有两个值。 怎么弄,我当然得到值为2呢?
网络科技 2013-12-22
  • 打赏
  • 举报
回复
引用 8 楼 u011461314 的回复:
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);
		    	 
		    	 
		    	 
		     }}
		 });
		 
	});
这个方法可行,谢了,结了
zhjdg 2013-12-22
  • 打赏
  • 举报
回复
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);
		    	 
		    	 
		    	 
		     }}
		 });
		 
	});
网络科技 2013-12-22
  • 打赏
  • 举报
回复
原因找到了,确实是延迟执行ajax的原因,导致myStore值还没得到,就执行了myStore.getCount();所以值为0,我延时了10秒后,基本上都能得到值,这也解释了为何我延时3秒,有时能得到值,有时得不到值的原因了。 但是解决方法用这个延时貌似不科学啊,也不可靠,一个是延时时间这个并不是固定的,如果返回的数据多,则后台执行时间会更长。 不知,有没办法,先返回数据后,即mystore得到值后,再往下执行呢,相当于阻塞模式,或同步什么的,水平有限,不知道怎么写,请大侠指教。
网络科技 2013-12-22
  • 打赏
  • 举报
回复
引用 4 楼 u011461314 的回复:
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);
		 
		 

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

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧