torsten@66: Ext.define('Lada.view.widgets.LadaForm', { torsten@66: extend: 'Ext.form.Panel', torsten@66: torsten@66: alias: 'widget.ladaform', torsten@66: /** torsten@66: * http://moduscreate.com/expert-ext-js-model-integration-in-forms/ torsten@66: */ torsten@66: torsten@66: /** torsten@66: * Can be a reference to a model instance or a model class name. torsten@66: */ torsten@66: model: null, torsten@66: /** torsten@66: * Set to the id of the model instance and the model will be loaded for you. torsten@66: * Only applicable if model provided is a model class name (string). torsten@66: */ torsten@66: modelId: null, torsten@66: bodyPadding: '10 10', torsten@66: border: 0, torsten@66: torsten@75: errors: null, torsten@75: warnings: null, torsten@75: message: null, torsten@133: success: null, torsten@87: readonly: false, torsten@75: torsten@66: initComponent: function() { torsten@66: torsten@66: this.callParent(); torsten@66: torsten@66: this.getForm().trackResetOnLoad = true; //Workaround torsten@66: torsten@66: if (Ext.isString(this.model)) { torsten@66: torsten@66: //Load a model to be updated torsten@66: if (this.modelId) { torsten@66: torsten@66: Ext.ClassManager.get(this.model).load(this.modelId, { torsten@66: failure: this.onModelLoadFailure, torsten@66: success: this.onModelLoadSuccess, torsten@66: scope: this torsten@66: }); torsten@66: torsten@66: //Load an empty record to be inserted torsten@66: } else { torsten@66: this.bindModel(Ext.create(this.model, {})); torsten@66: } torsten@66: torsten@66: } else { torsten@66: torsten@66: //Bind the provided model to be updated torsten@66: this.bindModel(this.model); torsten@66: torsten@66: } torsten@66: torsten@66: this.addEvents('loadsuccess', 'loadfailure', 'savesuccess', 'savefailure'); torsten@66: }, torsten@66: torsten@66: bindModel: function(model) { torsten@66: this.model = model; torsten@66: this.loadRecord(model); torsten@66: }, torsten@66: torsten@66: commit: function(callback, scope) { torsten@84: if (this.form.isDirty() && this.form.isValid()) { torsten@66: this.form.updateRecord(this.model); torsten@66: torsten@133: var data = this.model.getAllData(); torsten@133: var baseUrl = this.model.getProxy().url; torsten@133: var url = baseUrl; torsten@133: var method = "POST"; torsten@133: if (this.model.getId()) { torsten@133: url += this.model.getEidi(); torsten@133: method = "PUT"; torsten@133: } torsten@133: torsten@133: Ext.Ajax.request({ torsten@133: url: url, torsten@133: jsonData: data, torsten@133: method: method, torsten@133: callback: function(option, success, response) { torsten@133: this.parseResponse(response); torsten@133: if (this.success) { torsten@66: console.log('Save was successfull'); torsten@133: this.fireEvent('savesuccess', this); torsten@66: } else { torsten@66: console.log('Save was not successfull'); torsten@75: this.form.markInvalid(this.errors); torsten@133: this.fireEvent('savefailure', this); torsten@66: } torsten@66: }, torsten@66: scope: this torsten@66: }); torsten@133: torsten@133: //this.model.save({ torsten@133: // callback: function(records, operation) { torsten@133: // this.parseResponse(operation); torsten@133: // if (operation.wasSuccessful()) { torsten@133: // console.log('Save was successfull'); torsten@133: // this.fireEvent('savesuccess', this, records, operation); torsten@133: // } else { torsten@133: // console.log('Save was not successfull'); torsten@133: // this.form.markInvalid(this.errors); torsten@133: // this.fireEvent('savefailure', this, records, operation); torsten@133: // } torsten@133: // if (callback) { torsten@133: // callback.call(scope || this, this, operation.wasSuccessful(), this.model); torsten@133: // } torsten@133: // }, torsten@133: // scope: this torsten@133: //}); torsten@66: } torsten@66: }, torsten@66: torsten@66: onModelLoadSuccess: function(record, operation) { torsten@66: this.bindModel(record); torsten@83: this.parseResponse(operation); torsten@66: this.fireEvent('loadsuccess', this, record, operation); torsten@66: }, torsten@66: torsten@66: onModelLoadFailure: function(record, operation) { torsten@83: this.parseResponse(operation); torsten@66: this.fireEvent('loadfailure', this, record, operation); torsten@74: }, torsten@74: torsten@74: translateReturnCodes: function(codes) { torsten@74: var translated = {}; torsten@74: for (var k in codes) { torsten@74: translated[k] = Lada.getApplication().bundle.getMsg(codes[k]); torsten@74: } torsten@74: return translated; torsten@83: }, torsten@85: setReadOnly: function (bReadOnly) { torsten@85: this.getForm().getFields().each (function (field) { torsten@85: //field.setDisabled(bReadOnly); torsten@85: field.setReadOnly(bReadOnly); torsten@85: }); torsten@85: }, torsten@133: parseResponse: function(response) { torsten@133: var json = Ext.decode(response.responseText); torsten@133: if (json) { torsten@133: this.success = json.success; torsten@133: this.errors = this.translateReturnCodes(json.errors); torsten@133: this.warnings = this.translateReturnCodes(json.warnings); torsten@133: this.message = Lada.getApplication().bundle.getMsg(json.message); torsten@185: this.setReadOnly(json.readonly); torsten@185: } else { torsten@185: this.setReadOnly(true); torsten@133: } torsten@66: } torsten@133: // This parse method is used if the model.save() method is used to trigger torsten@133: // a request on the server side. In this case the response object is torsten@133: // different. torsten@133: //parseResponse: function(operation) { torsten@133: // this.errors = this.translateReturnCodes(operation.request.scope.reader.jsonData["errors"]); torsten@133: // this.warnings = this.translateReturnCodes(operation.request.scope.reader.jsonData["warnings"]); torsten@133: // this.message = Lada.getApplication().bundle.getMsg(operation.request.scope.reader.jsonData["message"]); torsten@133: // //this.setReadOnly(true); torsten@133: //} torsten@66: torsten@66: });