-
23 Mar 2012 1:01 AM #1
Proxy url suffix and extra parameter for all proxies/request
Proxy url suffix and extra parameter for all proxies/request
The Geocongress app contains a proxy like:
I have a similar use case where my proxies will fetch data from a specific url and I also need to send an API key.Code:Ext.define('GeoCon.store.Bills', { extend : 'Ext.data.Store', config: { model: 'GeoCon.model.Bill', groupField: 'state', proxy: { type: 'jsonp', url: 'http://api.realtimecongress.org/api/v1/bills.json', // The following must be set to disable extra parameters being sent to the API, which breaks it noCache: false, startParam: '', pageParam: '', limitParam: '', extraParams: { apikey: '8a341f85c657435989e75c9a83294762' }, reader: { type: 'json', rootProperty: 'bills' } } } });
Instead of hardcoding the API key and server address for each proxy i want to store this information somewhere and automatically use it for all proxies.
I wonder what the best practices are for this, and maybe somebody knows an example.
After that I want to extend this and retrieve the server hostname and API key from the local storage.
This makes it possible to connect to e.g. a preprod server and use different API key per customer.
-
23 Mar 2012 6:53 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,582
- Vote Rating
- 433
For the different URLs I use a config class and you can do this for the api key. For example:
In your app.js you need to setup a path and require the file:Code:Ext.define('MyApp.util.Config', { singleton : true, config : { api : '....', someUrl : 'path/to/service' } });
Now in your stores you can do this:Code:Ext.Loader.setPath({ MyApp : 'app' }); Ext.require([ 'MyApp.util.Config' ]);
Now you have one place for your api key and urls.Code:Ext.define('MyApp.store.Something', { extend : 'Ext.data.Store', config : { type : 'ajax', url : MyApp.util.Config.getSomeUrl(), extraParams : { api : MyApp.util.Config.getApi() }, reader : { type : 'json' } } });Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
23 Mar 2012 7:01 AM #3
Thanks for the reply! I will look into this.
-
26 Mar 2012 12:01 PM #4
PS:
I had to call it like
MyApp.util.Config.config.someUrl
Your example
MyApp.util.Config.getSomeUrl()
did not work for me. However this would look nicer.
Do you think it should work as you suggested?
-
26 Mar 2012 12:05 PM #5Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,582
- Vote Rating
- 433
Yeah, forgot to mention the initConfig call:
Code:Ext.define('MyApp.util.Config', { singleton : true, config : { api : '....', someUrl : 'path/to/service' }, constructor : function(config) { this.initConfig(config); this.callParent([config]); } });Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
26 Mar 2012 12:12 PM #6
Thanks for quick reply.
But unfortunately it's not working for me; see my code below
Any idea?Code:Ext.define('App.proxy.AppEngine', { extend: 'Ext.data.proxy.JsonP', alias: 'proxy.appengine', config: { extraParams: { secretKey: App.util.Config.getSecretKey() // DOES NOT WORK // secretKey: App.util.Config.config.secretKey WORKS } }, constructor: function(config) { this.initConfig(config); this.callParent([config]); }, getUrl: function(request) { return App.util.Config.config.serverUrl + this.superclass.getUrl.call(this, request); } }); Ext.define('App.util.Config', { singleton: true, config: { secretKey: 'thekey', // serverUrl: 'https://theapp.appspot.com' serverUrl: 'http://localhost:8888' } });
-
28 Mar 2012 4:36 AM #7
Actually this is working. I accidently added the constructor to the proxy

Thanks again for your help.


Reply With Quote