【问题标题】:how to wait for loading data into the store如何等待将数据加载到存储中
【发布时间】:2011-10-31 12:03:23
【问题描述】:

有菜单按钮(“客户”)、带有客户列表的树形面板(按名称排序)和带有选定客户详细信息的查看器。还有 selectionchange 动作..

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

我的控制器代码:

    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.getClientsListStore() 没有加载.. 那么如何检查这个商店的加载状态并等待这个商店完全自动加载?..

谢谢!

【问题讨论】:

    标签: javascript model-view-controller asynchronous extjs4 store


    【解决方案1】:

    您可以监听 store 'load' 事件。像这样:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-07
      • 2019-12-23
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      相关资源
      最近更新 更多