diff app/view/widgets/LadaForm.js @ 66:fcb63271d1bd

Created custom Form element which is bound to the model
author Torsten Irländer <torsten.irlaender@intevation.de>
date Wed, 05 Jun 2013 15:40:15 +0200
parents
children 5c58c0e8936b
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/view/widgets/LadaForm.js	Wed Jun 05 15:40:15 2013 +0200
@@ -0,0 +1,91 @@
+Ext.define('Lada.view.widgets.LadaForm', {
+    extend: 'Ext.form.Panel',
+
+    alias: 'widget.ladaform',
+    /**
+     * http://moduscreate.com/expert-ext-js-model-integration-in-forms/
+     */
+
+    /**
+     * Can be a reference to a model instance or a model class name.
+     */
+    model: null,
+    /**
+     * Set to the id of the model instance and the model will be loaded for you.
+     * Only applicable if model provided is a model class name (string).
+     */
+    modelId: null,
+    bodyPadding: '10 10',
+    border: 0,
+
+    initComponent: function() {
+
+        this.callParent();
+
+        this.getForm().trackResetOnLoad = true; //Workaround
+
+        if (Ext.isString(this.model)) {
+
+            //Load a model to be updated
+            if (this.modelId) {
+
+                Ext.ClassManager.get(this.model).load(this.modelId, {
+                    failure: this.onModelLoadFailure,
+                    success: this.onModelLoadSuccess,
+                    scope: this
+                });
+
+            //Load an empty record to be inserted
+            } else {
+                this.bindModel(Ext.create(this.model, {}));
+            }
+
+        } else {
+
+            //Bind the provided model to be updated
+            this.bindModel(this.model);
+
+        }
+
+        this.addEvents('loadsuccess', 'loadfailure', 'savesuccess', 'savefailure');
+    },
+
+    bindModel: function(model) {
+        this.model = model;
+        this.loadRecord(model);
+    },
+
+    commit: function(callback, scope) {
+        if (this.form.isDirty()) {
+            this.form.updateRecord(this.model);
+
+            this.model.save({
+                callback: function(records, operation) {
+                    if (operation.wasSuccessful()) {
+                        console.log('Save was successfull');
+                        this.fireEvent('savesuccess', this, records, operation);
+                    } else {
+                        console.log('Save was not successfull');
+                        var errors = operation.request.scope.reader.jsonData["errors"];
+                        this.form.markInvalid(errors);
+                        this.fireEvent('savefailure', this, records, operation);
+                    }
+                    if (callback) {
+                        callback.call(scope || this, this, operation.wasSuccessful(), this.model);
+                    }
+                },
+                scope: this
+            });
+        }
+    },
+
+    onModelLoadSuccess: function(record, operation) {
+        this.bindModel(record);
+        this.fireEvent('loadsuccess', this, record, operation);
+    },
+
+    onModelLoadFailure: function(record, operation) {
+        this.fireEvent('loadfailure', this, record, operation);
+    }
+
+});

http://lada.wald.intevation.org