在Ext的JsonStore的load方法中使用回调callback

Ajax的异步请求有时候会带来一些问题,如在Ext做的页面中,需要加载一个JsonStore数据集,在紧跟着的代码中可能就会对数据集进行访问的操作,由于异步请求很可能导致数据未加载完就开始访问,结果可能会不是想要的结果。这里load方法中提供了一个callback回调函数,可以解决延迟加载的问题,我们需要处理的一部分代码可以放在callback函数中处理,这样就解决了异步请求带来的问题。如:
var collectIndex_ds = new Ext.data.JsonStore({ 
url: '',
root: 'collectIndexs',
id: 'id',
fields:[
{name:'id'},
{name:'userViewId'},
{name:'timeType'},
{name:'measureUnitId'}
]
});

其回调函数使用为
//使用回调方法,可以解决延迟加载的问题;这里使用回调时为了把userViewId项为空的加上默认值1
collectIndex_ds.load({
callback :function(r,options,success){
if(success){
for(var i=0;i var record = r[i];
var v = record.data.userViewId;
if(v==null || v==""){
record.set("userViewId",1);
}
}
}
}
});

Ext官网关于callback的解释
[quote]callback : Function

A function to be called after the Records have been loaded. The callback is called after the load event and is passed the following arguments:

* r : Ext.data.Record[]
* options: Options object from the load call
* success: Boolean success indicator[/quote]


自动充值软件
[url][b]www.yisai5151.com[/b][/url]

你可能感兴趣的:(js技术)