Javascript 如何等待将数据加载到存储中

Javascript 如何等待将数据加载到存储中,javascript,model-view-controller,asynchronous,extjs4,store,Javascript,Model View Controller,Asynchronous,Extjs4,Store,有菜单按钮客户端、按名称排序的带有客户端列表的树面板和带有选定客户端详细信息的查看器。还有selectionchange操作 我的任务-在按钮上单击切换到客户端视图,并在每次单击按钮时选择并加载第一个客户端的详细信息。我的问题-存储未加载,如何等待ext js将数据自动加载到存储 我的控制器代码: me.control({ '#nav-client': { click: me.onNavClientClick }, ...

有菜单按钮客户端、按名称排序的带有客户端列表的树面板和带有选定客户端详细信息的查看器。还有selectionchange操作

我的任务-在按钮上单击切换到客户端视图,并在每次单击按钮时选择并加载第一个客户端的详细信息。我的问题-存储未加载,如何等待ext js将数据自动加载到存储

我的控制器代码:

    me.control({
        '#nav-client': {
            click: me.onNavClientClick
        },
    ...
        'clientlist': {
        //    load: me.selectClient,
            selectionchange: me.showClient
        }
    });

    onNavClientClick: function(view, records) {
        var me = this,
            content = Ext.getCmp("app-content");
        content.removeAll();
        content.insert(0, [{xtype: 'clientcomplex'}]);
        var first = me.getClientsStore().first();

        if (first) {
            Ext.getCmp("clientList").getSelectionModel().select(me.getClientsListStore().getNodeById(first.get('clientId')));
        }
    },
...
两个主要问题:

这对我来说是个好办法吗?选择树面板中的第一个客户端

var first = me.getClientsStore().first(); 
// i use another store to get first record because of i dont know how to get first record (on root level) in TreeStore
...
Ext.getCmp("clientList").getSelectionModel().select(me.getClientsListStore().getNodeById(first.get('clientId')));
我知道这段代码在加载时可以正常工作:me.selectClient,但只运行一次

如果我将此代码放在按钮上单击-我看到错误

Uncaught TypeError: Cannot read property 'id' of undefined
因为我。getClientsListStore未加载。。那么,如何检查此存储的加载状态并等待一些直到此存储完全自动加载


谢谢大家!

您可以侦听存储“加载”事件。像这样:

...
onNavClientClick: function(view, records) {
  var me = this;

  // if the store isn't loaded, call load method and defer the 'client view' creation
  if (me.getClientsStore.getCount() <= 0) {
    me.getClientsStore.on('load', me.onClientsStoreLoad, me, { single : true});
    me.getClientsStore.load();
  }
  else {
    me.onClientsStoreLoad();
  }
},

onClientsStoreLoad : function () {
    var me = this,
        content = Ext.getCmp("app-content");
    content.removeAll();
    content.insert(0, [{xtype: 'clientcomplex'}]);
    var first = me.getClientsStore().first();

    if (first) {
        Ext.getCmp("clientList").getSelectionModel().select(me.getClientsListStore().getNodeById(first.get('clientId')));
    }
},
...