2011-08-21 77 views
0

有菜单按钮(“客户端”),带客户端列表(按名称排序)的树面板和具有选定客户端详细信息的查看器。还有选择更改动作..如何等待将数据加载到商店中

我的任务 - 按钮单击切换到客户端视图,并选择并加载第一个客户端的细节,每次点击按钮。我的问题 - 商店没有加载,等到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 
    

因为me.getC的lientsListStore()没有加载..所以如何检查这家商店的加载状态,并等待一些,直到这家商店将完全自动加载?..

谢谢!

回答

0

您可以收听商店“加载”事件。像这样:

... 
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'))); 
    } 
}, 
...