-
14 May 2012 7:56 AM #1
Answered: Dynamic Assignment of Extra Parameters in REST Proxy
Answered: Dynamic Assignment of Extra Parameters in REST Proxy
We are building an app using PhoneGap + Sencha 2.x. The app will allow potentially multiple users to login and logout while the app is running. As part of the server API we've built, we assign a session id to the app. Pretty plain stuff.
In order to get the session id to our API for authentication, since PhoneGap does not handle cookies, we need to send extra parameters to our API. Thus far we've been doing this via a custom proxy writer we've developed. Now we want to transition to using Sencha Architect, which can't handle custom writers. How then can we update the model and store definitions with a new session id assigned by the server (when a new user logs in) so that future requests will send the correct session id?
I have one idea, but it would require us modifying our copy of the Sencha code. We could assign a function as a value within the extraParams object of the proxy. The code modification would be where Sencha comiles the extra parameters for the proxy. Adding a foreach loop that would check for callbacks to build dynamic values would be the solution. Is this overkill or does Sencha already offer a solution to meet our needs?
Thank you,
Todd
-
Best Answer Posted by mitchellsimoens
Correct. In the store, here it the applyProxy that sets the proxy on the store, notice in red:
Code:applyProxy: function(proxy, currentProxy) { proxy = Ext.factory(proxy, Ext.data.Proxy, currentProxy, 'proxy'); if (!proxy && this.getModel()) { proxy = this.getModel().getProxy(); } if (!proxy) { proxy = new Ext.data.proxy.Memory({ model: this.getModel() }); } if (proxy.isMemoryProxy) { this.setSyncRemovedRecords(false); } return proxy; },
-
16 May 2012 4:54 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,684
- Vote Rating
- 435
- Answers
- 3111
You could just have a beforeload event listener on the store to set the extra param to have your session data.
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.
-
16 May 2012 5:35 AM #3
Thank you for your response. I can see in loading a store how that would work, but when making changes to models persistent via a REST call, one would need to use the save() method on a model. This would mean that the model's proxy would be used, and any extraParams would be from the initial load/define or autoload of the Model definition That is, unless, one were to set the extraParams argument on a model's proxy before every save operation performed on model, since there are no events on models as is the case with stores. That would be an error prone methodology, as developers would sometimes forget to modify the proxy values.
I fall back to my original plan of attack -- to modify the processing of the extraParams to allow a value to be a function which will be called to dynamically set the values at runtime. Again, unless there is some other methodology that I am not yet aware of.
-
16 May 2012 5:41 AM #4Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,684
- Vote Rating
- 435
- Answers
- 3111
If you set the proxy on the model and not on the store, the store will use the proxy from the model therefore sharing the same proxy instance.
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.
-
16 May 2012 5:52 AM #5
To make sure that your communication is effective, meaning that I understand what you meant -- By setting the proxy on the model, when a store loads the model instances through a REST request, updating the extraParams via an onbeforeload event will change the params in the model proxy definitions. Therefore, when the save() method is called for an individual model, the extraParams will have been updated already, and the correct sessionId (the extra parameter we care about) will be available for the request.
Again, I thank you for your responses.
-
16 May 2012 5:58 AM #6Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,684
- Vote Rating
- 435
- Answers
- 3111
Correct. In the store, here it the applyProxy that sets the proxy on the store, notice in red:
Code:applyProxy: function(proxy, currentProxy) { proxy = Ext.factory(proxy, Ext.data.Proxy, currentProxy, 'proxy'); if (!proxy && this.getModel()) { proxy = this.getModel().getProxy(); } if (!proxy) { proxy = new Ext.data.proxy.Memory({ model: this.getModel() }); } if (proxy.isMemoryProxy) { this.setSyncRemovedRecords(false); } return proxy; },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.


Reply With Quote