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@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@66: if (this.form.isDirty()) { torsten@66: this.form.updateRecord(this.model); torsten@66: torsten@66: this.model.save({ torsten@66: callback: function(records, operation) { torsten@66: if (operation.wasSuccessful()) { torsten@66: console.log('Save was successfull'); torsten@66: this.fireEvent('savesuccess', this, records, operation); torsten@66: } else { torsten@66: console.log('Save was not successfull'); torsten@66: var errors = operation.request.scope.reader.jsonData["errors"]; torsten@66: this.form.markInvalid(errors); torsten@66: this.fireEvent('savefailure', this, records, operation); torsten@66: } torsten@66: if (callback) { torsten@66: callback.call(scope || this, this, operation.wasSuccessful(), this.model); torsten@66: } torsten@66: }, torsten@66: scope: this torsten@66: }); torsten@66: } torsten@66: }, torsten@66: torsten@66: onModelLoadSuccess: function(record, operation) { torsten@66: this.bindModel(record); torsten@66: this.fireEvent('loadsuccess', this, record, operation); torsten@66: }, torsten@66: torsten@66: onModelLoadFailure: function(record, operation) { torsten@66: this.fireEvent('loadfailure', this, record, operation); torsten@66: } torsten@66: torsten@66: });