2017-05-04 65 views
2

我有一個關於ExtJS控制器的問題。我的代碼:使用extjs控制器

Ext.define('app.controller.Clients.Clients', { 
    extend: 'Ext.app.Controller', 
    stores: ['Clients.Clients'], 
    models: ['Clients.Clients'], 
    views: ['Clients.Clients'], 
    init: function() { 
     this.control({ 
      'gridClients button[action=deleteClient]': { 
       click: this.onButtonClickDelete 
      }, 
      'gridClients button[action=refreshClients]': { 
       click: this.onButtonClickRefresh 
      }, 
      'gridClients button[action=printClients]': { 
       click: this.onButtonClickPrint 
      } 
     }) 
    }, 
    onButtonClickDelete: function(button, e, options) { 
     alert('DELETE?'); 
    }, 
    onButtonClickRefresh: function(button, e, options) { 
     alert('REFRESH?'); 
    }, 
    onButtonClickPrint: function(button, e, options) { 
     alert('PRINT?'); 
    } 
}); 

我要引用一個名爲「gridClients」網格,我想知道是否有任何的方式來創建驅動程序文件中的變量...

我將參考一個名爲'gridClients'的網格,並且我想知道是否有任何方法在驅動程序文件內創建一個變量來引用該網格。

例子,我想類似的東西:

Var Grid = Ext.ComponentQuery.query (#gridClients) [0]; 

而且使用這樣的:

OnButtonClickRefresh: function (button, e, options) { 
     Grid.getStore(). Load(); 
    } 

我真的不知道從哪裏聲明VAR ...

回答

1

在控制器,您預計與refs工作。例如:

Ext.define('app.controller.Clients.Clients', { 
    extend: 'Ext.app.Controller', 
    stores: ['Clients.Clients'], 
    models: ['Clients.Clients'], 
    views: ['Clients.Clients'], 
    init: function() { 
     ... 
    }, 
    refs:[{ 
     ref:'clientsGridExample', 
     selector: '#gridClients' 
    }], 
    OnButtonClickRefresh: function (button, e, options) { 
     this // inside a controller, these functions are scoped to controller 
      .getClientsGridExample() // refs are automatically converted to getter methods 
      .getStore().load(); // Do whatever you want to do 
    } 
}); 
+0

太棒了! 它的工作原理,感謝您在閱讀文檔後向我展示'refs'的用法,我理解它的用法。 –

0

在OnButtonClickRefresh監聽器中,您可以像這樣得到網格:

var grid = button.up("grid"); 

鏈接到fiddle

1

這是很清楚的,如果你檢查Ext.app.Controller documentation

您可以在您的控制器中設置refs並使用生成的getter來獲取所需的網格。例如,如果您的ref的值爲clientsGrid,則ExtJS將創建獲取者getClientsGrid()

`

Ext.define('app.controller.Clients.Clients', { 
    extend: 'Ext.app.Controller', 
    stores: ['Clients.Clients'], 
    models: ['Clients.Clients'], 
    views: ['Clients.Clients'], 

    refs: [ 
     { ref: 'grid', selector: '#gridClients' } 
    ],  

    init: function() { 
     this.control({ 
      'gridClients button[action=deleteClient]': { 
       click: this.onButtonClickDelete 
      }, 
      'gridClients button[action=refreshClients]': { 
       click: this.onButtonClickRefresh 
      }, 
      'gridClients button[action=printClients]': { 
       click: this.onButtonClickPrint 
      } 
     }) 
    }, 
    onButtonClickDelete: function(button, e, options) { 
     this.getGrid().doSomething(); 
    }, 
    onButtonClickRefresh: function(button, e, options) { 
     alert('REFRESH?'); 
    }, 
    onButtonClickPrint: function(button, e, options) { 
     alert('PRINT?'); 
    } 
}); 

`