User.js 1.64 KB
Ext.define('MyMA.controller.User', {
    extend: 'Ext.app.Controller',
    views: ['User'],
    refs: [{
        selector: 'user',
        ref: 'userWindow'
    }],
    
    init: function() {
        this.control({
            'user > toolbar > #savedata': {
                click: this.saveData
            }
        });
    },
    
    saveData: function(Button) {
        var form = this.getUserWindow().down('form').getForm(),
            id = form.getValues().id || null;

        if(!form.isValid()) {
            return false;
        }

        form.submit({
        	url: Ext.Ajax.getRestUrl('api','user', id),
            clientValidation: true,
            method: id > 0 ? 'PUT' : 'POST',
            params: Ext.applyIf(Ext.copyTo({}, form.getValues(), 'smtp,imap,manager'), {
                smtp: 0,
                imap: 0,
                manager: 0
            }),
            scope: {
                controller: this,
                win: this.getUserWindow()
            },
            success: this.onSuccessSubmit,
            failure: this.onFailSubmit
        });
    },


    /**
     *
     */
    onSuccessSubmit: function(form, action) {
        this.win.close();
        this.controller.getController('Users').getStore('Users').reload({
            params: {
                start: 0
            }
        });
        Ext.Msg.showInfo({
            msg: 'Data saved'
        })
    },


    /**
     *
     */
    onFailSubmit: function(form, action) {
    	try {
    		var data = Ext.decode(action.response.responseText);
    		throw(data);
    	}
    	catch(e) {
            Ext.Msg.showError({
            	msg: e
            });
    	}
    }
});