-
30 Nov 2012 8:51 AM #1
Unanswered: Different rootProperty for each action (create/read/update/destroy)
Unanswered: Different rootProperty for each action (create/read/update/destroy)
Hello again! I'm sorry, I'm on a request spree today

I'm dealing with a rest service that returns data with "messages" as the rootProperty when loading multiple items (/messages), but returns "message" when working with a single item (/messages/<id>).
If I create a new item using the model object (record.save()), I need it to do a POST with a rootProperty "message". The server then returns the record, and Sencha updates the model object with the id specified by the server. So far, so good.
However, when adding the above record in a store (store.add(record)), the store's sync method will do a POST to the server with a rootProperty "message", which is right, but will not read the data returned by the server because the store reader expects "messages", while the server returns "message" instead.
Is there a way to specify different rootProperty for each action? Or, how do you solve this issue? Keep in mind that I cannot change how the server returns the data.
Thanks in advance
-
2 Dec 2012 7:50 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,684
- Vote Rating
- 435
- Answers
- 3111
You can change the rootProperty value on the writer before you save or load or sync.
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.
-
3 Dec 2012 3:57 AM #3
Doesn't seem to work, I don't get why. The rootProperty is changed but the record is not updated with the server generated id.
Here's how I did it.
Reader:
Proxy:Code:Ext.define('My.JsonReader', { extend: 'Ext.data.JsonReader', alias : 'reader.myreader', config: { rootProperties: null } });
Store:Code:Ext.define('My.Proxy', { extend: 'Ext.data.proxy.Rest', alias: 'proxy.myproxy', doRequest: function(operation, callback, scope){ var reader = this.getReader(); var roots = reader._rootProperties; var action = operation.getAction(); if (roots !== undefined && roots !== null && roots !== false && roots[action]) { reader.setRootProperty(roots[action]); } this.callParent(arguments); } });
Help?Code:Ext.define('My.MessagesStore', { extend: 'Ext.data.Store', config: { model: 'My.Message', proxy: { type: 'myproxy', url: '/messages', reader: { type: 'myreader', rootProperties: { create: 'message', read: 'messages', update: 'message', destroy: 'message' } }, writer: { type: 'json', rootProperty: 'message' } } } });
-
3 Dec 2012 4:08 AM #4
I forgot to add: the store is set to autoSync, and the record is saved successfully, but launching store.sync() will make the store save it again.


Reply With Quote