Users.js 2.75 KB
Ext.define('MyMA.controller.Users', {
    extend: 'Ext.app.Controller',
	stores: ['Users', 'Choice'],
	views: ['Users', 'User'],
	refs: [{
		selector: 'users > grid',
		ref: 'usersList'
	}, {
	    selector: 'users',
	    ref: 'usersWindow'
	}],
	
    init: function() {
		this.control({
			'users > grid > toolbar > #addrecord': {
				click: this.showForm
            },
            'users > grid > toolbar > #search': {
                click: this.onSearch
            },
            'users actioncolumn': {
                click: this.onActionColumn
            },
            'users > grid': {
                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-edit')) {
            var record = scope.store.getAt(rowIndex);

            var widget = this.getController('Taskpanel').addProgram({
                name: 'user'
            });

            widget.setTitle('User: ' + record.get('name'));
            widget.down('form').getForm().loadRecord(record);
            widget.down('form').down('combo[name=domid]').getStore().reload();
        }
        if(e.getTarget('.x-ibtn-delete')) {
            var record = scope.store.getAt(rowIndex);
            
            Ext.Msg.confirm('Info', 'Press Yes to conform remove action', function(button) {
                if (button === 'yes') {
                    this.record.destroy({
                        scope: this,
                        success: function() {
                            this.store.remove(this.record)
                        }
                    });
                }
            }, {
                store: scope.store,
                record: record
            });
        }
    },

	
	/**
	 * Show form to add new record or edit existing data
     * @param {Object} Button
	 */
	showForm: function(Button) {
		var widget = this.getController('Taskpanel').addProgram({
            name: 'user',
            title: 'New User'
        });
        widget.down('form').down('combo[name=domid]').getStore().reload();
	},
	
	
	/**
     * Search action
     */
    onSearch: function(Button) {
        var store = this.getUsersList().getStore();
        store.getProxy().extraParams = Ext.apply({}, Button.up('toolbar').getValues());
        store.reload({
            params: { }
        });
    },
	
	
	/**
     * Fires when widget is shown
     */
    onGridRender: function(grid) {
        grid.getStore().reload();
    }
});