0

我有一个带有商店、模型和视图(工具栏)的控制器:

Ext.define('Cc.controller.Headers', {
  extend: 'Ext.app.Controller',

  stores: ['User'],
  models: ['Agent'],
  views: ['Header'],


  refs: [
    { ref: 'navigation', selector: 'navigation' },
    { ref: 'tabPanel', selector: 'tabpanel' },
    { ref: 'head', selector: 'head' },
    { ref: 'logoutButton', selector: 'head button[action=logout]'},
    { ref: 'helpButton', selector: 'head button[action=help]'}
  ],

  init: function() {
    this.control({
      'head button[action=logout]': {
        beforerender: this.initLogoutButton,
        click: this.onClickLogoutButton
      },
      'head button[action=help]':{
        click: this.onClickHelpButton
      }
    });
  },

  initLogoutButton: function(a){
    var store = this.getUserStore(),
      button = this.getLogoutButton();
    store.load();
    var user = this.getAgentModel();
    button.setText('Logout ['+user.get('lastname')+']');
    console.log(store.count());
  },

  onClickLogoutButton: function(view, record, item, index, e){
    alert('déconnexion');
  },

  onClickHelpButton: function(view, record, item, index, e){
    alert('help');
  }
});

我的问题出在initLogoutButton函数中。我需要检索从 ajax 请求中获得的唯一用户实例(始终只有一个实例)。我可以在 js 控制台中看到请求,一切都很好,但是我不能用这个数据设置变量或数组!

此外,store.count()函数返回0

4

1 回答 1

0

如果您使用代理将数据加载到存储中,则应注意请求是异步的!您需要在商店加载数据后设置按钮文本。

store.load({
    scope   : this,
    callback: function(records, operation, success) {
        // check for success and set the value of button        
    }
});

加载商店后调用回调函数,然后设置按钮值。

于 2011-05-17T10:39:37.753 回答