我有一个卡片布局和卡片上的"activate"事件我加载商店。为了防止每次激活该卡时加载存储,我检查getCount == 0,如果为真,则加载存储:
handleActivateGrid: function(){
if(this.myTreeGrid().getStore().getCount() == 0){
this.myTreeGrid().getStore().load();
}
我在代码的其他地方使用了同样的方法,它工作得很好,唯一的区别是在这种情况下它是一个Ext.data.TreeStore。
我调试了,getCount()是未定义的,即使在store加载之后。
我可以使用其他方法或方法来实现上述内容吗?
感谢编辑:刚刚检查了文档,getCount()不是Ext.data中存在的方法。TreeStore,这就解释了
TreeStore由单个根节点和子节点组成。
要查看根节点下是否有节点,可以使用以下命令
myTreeGrid().getStore().getRootNode().childNodes.length > 0
http://docs-origin.sencha.com/extjs/4.2.2/见!/api/Ext.data.NodeInterface-property-childNodes
更正确的方法(因为理论上加载可能不加载任何子节点)是在初始创建时将加载事件与存储连接起来。在加载处理程序中,您可以在代码方便的地方设置isLoaded
标志。
比如http://www.sencha.com/forum/showthread.php?91923-How-to-check-datastore-is-loaded/page2
var storeLoaded = false;
var store = Ext.create('Ext.data.TreeStore', {
proxy: {
type: 'ajax',
url: 'get-nodes.php'
},
root: {text: 'Ext JS',id: 'src', expanded: true},
listeners: {
load: function() {
storeLoaded = true;
}
}
});
var tree = Ext.create('Ext.tree.Panel', {
store: store,
renderTo: 'tree-div',
height: 300,
width: 250,
title: 'Files'
});
刚刚试过这段代码。
myTreeGrid().getStore().getRootNode().getChildAt(0) == undefined