From 92a3c59be81ed141eaed7059139cfad534a238da Mon Sep 17 00:00:00 2001
From: Renaud Le Gac <renaud.legac@free.fr>
Date: Sun, 20 Mar 2011 16:50:21 +0000
Subject: [PATCH] Add a  "global variable" App.storeCfgs. It is fill by the
 controller get_api.

---
 controllers/plugin_dbui.py        | 14 +++++++++--
 static/plugin_dbui/src/appbase.js | 41 +++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/controllers/plugin_dbui.py b/controllers/plugin_dbui.py
index 14218d32..5c922ad6 100644
--- a/controllers/plugin_dbui.py
+++ b/controllers/plugin_dbui.py
@@ -32,7 +32,8 @@ App.REMOTE_API = {
     'url': '/%s/plugin_dbui/call',
     'type': 'remoting',
     'actions': %s 
-}; 
+};
+App.storeCfgs = %s;
 App.withMathJax = %s; """
 
 
@@ -128,6 +129,11 @@ def get_api():
             di[action] = []
             
         di[action].append({'name': method, 'len': nargs})
+  
+    # the stores configuration
+    storeCfgs = {}
+    for table in cfgSvc.get_tables()['tables']:
+        storeCfgs[table] = cfgSvc.get_jsonstore(table)
         
     # determine if the MathJax plugin is there
     server_path, client_path = dbui.get_reference_paths(globals())
@@ -136,7 +142,11 @@ def get_api():
 
     # fill the javascript template
     app = request.application
-    script = SCRIPT % (app, app, json.dumps(di), str(withMathJax).lower())
+    script = SCRIPT % (app, 
+                       app, 
+                       json.dumps(di),
+                       json.dumps(storeCfgs), 
+                       str(withMathJax).lower())
     
     # return the response as a javascript
     response.headers['Content-Type'] = 'text/javascript'
diff --git a/static/plugin_dbui/src/appbase.js b/static/plugin_dbui/src/appbase.js
index 07f64673..c74969a8 100644
--- a/static/plugin_dbui/src/appbase.js
+++ b/static/plugin_dbui/src/appbase.js
@@ -14,6 +14,11 @@ Ext.namespace('App');
  * This constants is defined by the server.
  */
 
+/**
+ * @cfg {Array} App.storeCfgs
+ * Configuration option for all json stores
+ */
+
 /**
  * @cfg {String} App.dburl
  * Define the URL of the database router/controller
@@ -40,6 +45,42 @@ App.encodeField = function (table, field) {
     return t + f;
 }
 
+/**
+ * Helper function returning the App.data.DirectStore identifies by its id.
+ * If the store does not exit it is created and register in the store manager.
+ * 
+ * NOTE: The storeId is defined by the server as a configuration option 
+ * for each widget: combobox, form and grid. 
+ * The syntax of the storeId is "tableStore". 
+ * If the store does not exist, its configuration is extract from 
+ * the application constant App.storeCfgs.Then it is created and registered.
+ * In the current frame work a unique store is associated to a database table.
+ * This property garanty that widget share the same store.
+ * 
+ * @param {String} storeId
+ */
+App.getDirectStore = function (storeId) {
+    
+    var cfg,
+        store,
+        table;
+    
+    store = Ext.StoreMgr.lookup(storeId);
+    
+    if (!store && !Ext.isString(storeId)) {
+        throw new Error('Fail to instanciate the store: "'+storeId+'"');
+    }    
+
+    if (!store && Ext.isString(storeId)) {
+
+        table = storeId.slice(0, storeId.search('Store'));
+        cfg = App.storeCfgs[table];
+        store = new App.data.DirectStore(cfg);
+    }
+
+    return store;
+}
+
 /**
  * Helper function returning the name of the database table 
  * associated to the store. works with store configured using the
-- 
GitLab