Created
November 27, 2012 01:10
-
-
Save jasoet/4151746 to your computer and use it in GitHub Desktop.
User.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ext.define('Rima.controller.User', { | |
extend:'Ext.app.Controller', | |
views:[ 'user.List', 'user.Edit' ], | |
allowWrite:Rima.util.SecurityHelper.isWriteAllowed("USER_WRITE"), | |
models:[ 'User', 'Jabatan', 'UnitSatuanKerja'], | |
stores:[ 'Users', 'Jabatan', 'UnitSatuanKerja'], | |
refs:[ | |
{ | |
ref:'userList', | |
selector:'userList' | |
}, | |
{ | |
ref:'userEditForm', | |
selector:'userEdit > form' | |
}, | |
{ | |
ref:'userGroupCheckbox', | |
selector:'userEdit checkboxgroup' | |
}, | |
{ | |
ref:'jabatanCombobox', | |
selector:'userEdit combobox[name=jabatan]' | |
}, | |
{ | |
ref:'unitSatuanKerjaCombobox', | |
selector:'userEdit combobox[name=unitSatuanKerja]' | |
}, | |
{ | |
ref:'userEditWindow', | |
selector:'userEdit' | |
}, | |
{ | |
ref:'userDeleteButton', | |
selector:'userList button[action=delete]' | |
} , | |
{ | |
ref:'userPagingToolbar', | |
selector:'userList pagingtoolbar' | |
} | |
], | |
init:function () { | |
this.control({ | |
'userList':{ | |
itemdblclick:this.editUser | |
} | |
}); | |
if (this.allowWrite) { | |
this.control({ | |
'userList':{ | |
itemclick:this.enableButton, | |
selectionchange:this.userListSelectionChange | |
}, | |
'userEdit button[action=save]':{ | |
click:this.updateUser | |
}, | |
'userList button[action=add]':{ | |
click:this.createUser | |
}, | |
'userList button[action=delete]':{ | |
click:this.deleteUser | |
} | |
}); | |
} else { | |
this.control({ | |
'userList':{ | |
show:function (self) { | |
self.down("button[action=add]").setDisabled(true); | |
self.down("button[action=delete]").setDisabled(true); | |
} | |
}, | |
'userEdit':{ | |
show:function (self) { | |
self.down("button[action=save]").setDisabled(true); | |
} | |
} | |
}); | |
} | |
}, | |
userListSelectionChange:function (sel, selected) { | |
if (selected[0]) { | |
this._toggleDeleteButton(true); | |
} else { | |
this._toggleDeleteButton(false); | |
} | |
}, | |
enableButton:function (button, record) { | |
this._toggleDeleteButton(true); | |
}, | |
editUser:function (grid, record) { | |
var view = Ext.widget('userEdit'); | |
view.down('form').loadRecord(record); | |
this._updateFormRecord(record.data); | |
}, | |
_updateFormRecord:function (data) { | |
if (data) { | |
var jabatanCombobox = this.getJabatanCombobox(); | |
var unitSatuanKerjaCombobox = this.getUnitSatuanKerjaCombobox(); | |
if (data.jabatan) { | |
jabatanCombobox.setValue(data.jabatan.id); | |
} | |
if (data.unitSatuanKerja) { | |
unitSatuanKerjaCombobox.setValue(data.unitSatuanKerja.id); | |
} | |
var groupsCheckbox = this.getUserGroupCheckbox(); | |
Ext.each(data.groups, function (group) { | |
groupsCheckbox.down('[inputValue=' + group.id + ']').setValue(true); | |
}); | |
} | |
}, | |
createUser:function () { | |
Ext.widget('userEdit'); | |
}, | |
deleteUser:function (button) { | |
var record = this.getUserList().getSelectionModel().getSelection()[0]; | |
var self = this; | |
if (record) { | |
Ext.Ajax.request({ | |
url:Rima.config.userUrl + record.data.id, | |
method:'DELETE', | |
headers:{ | |
'Content-Type':'application/json' | |
}, | |
success:function (response, options) { | |
self.getController('MessageBox').showMessageBoxDelete(response, options, 'User'); | |
self._doGridRefresh(); | |
}, | |
failure:function (response, options) { | |
self.getController('MessageBox').showMessageBoxDelete(response, options, 'User'); | |
} | |
}); | |
} | |
}, | |
updateUser:function (button) { | |
var self = this; | |
var form = this.getUserEditForm(); | |
var values = form.getValues(); | |
var record = form.getRecord(); | |
var groupsTemp = []; | |
Ext.each(values.groups, function (group) { | |
Ext.Array.push(groupsTemp, { | |
id:group | |
}); | |
}); | |
values.groups = groupsTemp; | |
if (values.jabatan) { | |
values.jabatan = { | |
id:values.jabatan | |
}; | |
} else { | |
values.jabatan = null; | |
} | |
if (values.unitSatuanKerja) { | |
values.unitSatuanKerja = { | |
id:values.unitSatuanKerja | |
}; | |
} else { | |
values.unitSatuanKerja = null; | |
} | |
//TODO Pengecekan, apabila Membuat User baru maka Password harus disertakan, apabila update maka password tidak wajib disertakan | |
values.password = Ext.isEmpty(values.pass) ? null : values.pass; | |
delete values.pass; | |
delete values.passConfirm; | |
if (form.getForm().isValid()) { | |
if (record) { | |
Ext.Ajax.request({ | |
url:Rima.config.userUrl + record.data.id, | |
method:'PUT', | |
headers:{ | |
'Content-Type':'application/json' | |
}, | |
params:Ext.JSON.encode(values), | |
success:function (response, options) { | |
self.getController('MessageBox').showMessageBoxPostPut(response, options, 'User'); | |
self._doGridRefresh(); | |
}, | |
failure:function (response, options) { | |
self.getController('MessageBox').showMessageBoxPostPut(response, options, 'User'); | |
} | |
}); | |
} else { | |
Ext.Ajax.request({ | |
url:Rima.config.userUrl, | |
method:'POST', | |
headers:{ | |
'Content-Type':'application/json' | |
}, | |
params:Ext.JSON.encode(values), | |
success:function (response, options) { | |
self.getController('MessageBox').showMessageBoxPostPut(response, options, 'User'); | |
self._doGridRefresh(); | |
}, | |
failure:function (response, options) { | |
self.getController('MessageBox').showMessageBoxPostPut(response, options, 'User'); | |
} | |
}); | |
} | |
this.getUserEditWindow().close(); | |
} | |
}, | |
_toggleDeleteButton:function (enable) { | |
var button = this.getUserDeleteButton(); | |
if (enable) { | |
button.enable(); | |
} else { | |
button.disable(); | |
} | |
}, | |
_doGridRefresh:function () { | |
this.getUserList().down('pagingtoolbar').doRefresh(); | |
} | |
}); |
Kaip cia daryt????
ahhhh
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
d