PDA

View Full Version : Rest Proxy and Different Roots for Different Operations (List/Create), autoSync



okwei
17 Jul 2013, 12:17 PM
How do I configure different 'root' for List (GET) and Add (POST) operations? Extjs 4.2



var store = new Ext.data.TreeStore({
model: 'folder',
autoSync: true,
proxy: {
type: 'rest',
url: '/service/folders',
appendId: true,
reader: {
type: 'json',
root: 'subFolders'
},
writer: {
type: 'json',
nameProperty: 'mapping',
root: 'folder'
}
},
root: {
text: 'My Folders',
id: '1',
expanded: true
}
});


The service returns, For listing (GET /service/folders/{id})


{
subFolders[{
id: 1,
text: 'Folder A'
}]
}


For creation (POST /service/folders)


{
folder: {
id: 10,
text: 'Folder New'
}
}

slemmon
19 Jul 2013, 12:19 PM
Instead of using the url config:
http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.proxy.Rest-cfg-url

You can use the api config:
http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.proxy.Rest-cfg-api

Which will let you set different URLs for each action.

skirtle
22 Jul 2013, 12:41 PM
There's nothing out-of-the-box for this so I think your best bet would be to override a method on the reader. For example, you could maybe override buildExtractors to create your own getRoot function. There are alternative overrides you could try but that'd be where I'd start.

The override itself wouldn't need to know what type of request it is. Instead just pull out whichever property is present in the response.