From 593f0cadb97e74340a6c2beabf19379aaea609c3 Mon Sep 17 00:00:00 2001
From: Renaud Le Gac <renaud.legac@free.fr>
Date: Sun, 13 Mar 2011 16:58:40 +0000
Subject: [PATCH] Give more flexibility to create a store and add some
 protection.

---
 static/plugin_dbui/src/appform.js | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)

diff --git a/static/plugin_dbui/src/appform.js b/static/plugin_dbui/src/appform.js
index 9215665f..4a6c396f 100644
--- a/static/plugin_dbui/src/appform.js
+++ b/static/plugin_dbui/src/appform.js
@@ -21,8 +21,10 @@ App.form.FormPanel = Ext.extend(Ext.form.FormPanel, {
 
     /**
      * @cfg {Ext.data.Store} store the Ext.data.Store that the form should use.
-     * Mandatory configuration parameters.
+     * The store has to be defined since the actions rely on it.
      */
+    store: null,
+    
     /**
      * Predefined configuration options
      */
@@ -76,17 +78,14 @@ App.form.FormPanel = Ext.extend(Ext.form.FormPanel, {
         this.addFieldToolTipsListeners();
         
         // link the form to the data store
-        this.store = Ext.StoreMgr.lookup(this.store);
+        if (this.store) {
+            this.store = Ext.StoreMgr.lookup(this.store);
+            this.store.on('exception', this.onStoreException, this);
+            this.store.on('write', resetForm, this.getForm());    
+        }
 
         // set the default action: create
         this.setAction('create');
-        
-        // Store events listeners
-        this.store.on('exception', this.onStoreException, this);
-        
-        // always reset the form after a write
-        // allow to enter data in a batch mode
-        this.store.on('write', resetForm, this.getForm());
     },
 
     /**
@@ -118,7 +117,8 @@ App.form.FormPanel = Ext.extend(Ext.form.FormPanel, {
     /**
      * Handler to perform the current action on the store.
      * The current action is set by the setAction method.
-     * Should not be call directly, except for delete without confirmation. 
+     * Should not be call directly, except for delete without confirmation.
+     * Raise an error if the store is not defined.
      */
     doAction: function () {
 
@@ -129,6 +129,10 @@ App.form.FormPanel = Ext.extend(Ext.form.FormPanel, {
             return;
         }
         
+        if (!this.store) {
+            throw new Error('the store is undefined !!!');
+        }
+        
         switch (this.currentAction) {
 
             case 'create':
@@ -227,12 +231,10 @@ App.form.FormPanel = Ext.extend(Ext.form.FormPanel, {
         this.currentAction = action;
         this.currentRecord = record;
         
-        this.hardReset();
-        
         switch (action) {
 
             case 'create':
-            
+                this.hardReset();
                 this.buttonAction.setText(this.textCreate);
                 break;  
                 
@@ -243,6 +245,7 @@ App.form.FormPanel = Ext.extend(Ext.form.FormPanel, {
                 
             case 'duplicate':
                 this.buttonAction.setText(this.textDuplicate);
+                this.hardReset();
                 table = App.getTableName(this.store);
                 tableId = App.encodeField(table, 'id');
                 delete record.data[tableId];
-- 
GitLab