Transports.js 2.94 KB
Ext.define('MyMA.controller.Transports', {
    extend: 'Ext.app.Controller',
    stores: ['Transports'],
    views: ['Transports'],
    refs: [{
        selector: 'transports > grid',
        ref: 'transportsList'
    }],
    
    init: function() {
        this.control({
            'transports > grid > toolbar > #addrecord': {
                click: this.addRecord
            },
            'transports > grid > toolbar > #search': {
                click: this.onSearch
            },
            'transports actioncolumn': {
                click: this.onActionColumn
            },
            'transports > grid': {
            	edit: this.onEditAction,
            	afterrender: this.onGridRender
            }
        });
    },
    
    
    /**
     * Handles action columns click
     * @param {Object} grid view
     * @param {HTMLElement} Element
     * @param {Integer} row index
     * @param {Integer} column index
     * @param {Object} Event object
     * @param {Object} Scope object
     * @param {Object}
     */
    onActionColumn: function(gridview, el, rowIndex, colIndex, e, scope, rowEl) {
        if(e.getTarget('.x-ibtn-delete')) {
            var record = scope.store.getAt(rowIndex);
            
            Ext.Msg.confirm('Info', 'Press Yes to confirm remove action', function(button) {
                if (button === 'yes') {
                	if(!this.record.get('id')) {
                		this.store.remove(this.record);
                	}
                	else {
                        this.record.destroy({
                            scope: this,
                            success: function() {
                                this.store.remove(this.record)
                            }
                        });
                	}
                }
            }, {
                store: scope.store,
                record: record
            });
        }
    },
    
    
    /**
     * Add record to the store and start editing
     */
    addRecord: function(Button) {
    	this.getTransportsList().getStore().insert(0, this.getTransportsList().getStore().model.create({ 
               id: 0,
               domain: null,
               transport: 'virtual'
        }));
    },
    
    
    /**
     * Search action
     */
    onSearch: function(Button) {
        var store = this.getTransportsList().getStore();
        store.getProxy().extraParams = Ext.apply({}, Button.up('toolbar').getValues());
        store.reload({
            params: { }
        });
    },
    
    
    /**
     * Fires when edit complete and record was updated
     * @param   {object}, Ext.grid.plugin.Editing
     * @param   {object}, An edit event
     */
    onEditAction: function(editor, e) {
        e.record.save({
        	scope: e,
        	success: function(record) {
        		this.store.reload();
        	}
        });
    },
    
    
    /**
     * Fires when widget is shown
     */
    onGridRender: function(grid) {
        grid.getStore().reload();
    }
});