PDA

View Full Version : Ext.data.RestProxy & GET doesn't append id param



MWagner
10 Feb 2011, 1:17 AM
I'd like to use the Ext.data.RestProxy (http://dev.sencha.com/deploy/touch/docs/?class=Ext.data.RestProxy) with my Restful API.

The problem is: When I hand over the id of a selected item the id won't be appended to the request-uri.

the request-uri is always
/api/poi?_dc=1297...
and not
/api/poi/1
as expected.

My Restful api will return a set of items if no param is given. But I need the detailed data of a single item.

The idea is not to GET all detailed data of every item for the list view to reduce the traffic and to reload details when an item is selected.

I checked the vars, id is given correctly to the .load() function. Callback should work and my Restful api handles the requests correctly, too.

Is there something I forgot? Or could it be a bug in the .load()-function?

Model:

Ext.regModel('Poi', {
idProperty: 'id',

fields: [
'id',
'latitude',
'longitude',
'logo',
'title',
'description',
'article',
'video',
'audio',
'images'
],

proxy: {
type: 'rest',
id: 'id',
url : '/api/pois',
headers: {
project: project,
language: language
}
}

});

stores.poi = new Ext.data.Store({
model: 'Poi'
});

API call:


// POI List
app.poiListPanel = new Ext.List({
id : 'poiList',
scroll : 'vertical',
itemTpl : tplItem,
store : stores.pois.load(),
grouped : true,
onItemDisclosure: function(record) {
var Poi = Ext.ModelMgr.getModel('Poi');

// hand over the id of the selected list item to the RestProxy.
Poi.load(record.data.id, {
success: function(poi) {
console.log(poi.data);
app.poiDetailPanel.update(poi.data);
app.poiDetailMediaPanel.update(poi.data);
app.Viewport.setActiveItem('poidetailpanel');
}
});
}
});

Edit:
Checked the buildUrl-function for RestProxy. There seems to be an error in it. buildUrl requires a record. What should this be good for? IMO it makes no sense at all.



Ext.data.RestProxy = Ext.extend(Ext.data.AjaxProxy, {

appendId: true,




actionMethods: {
create : 'POST',
read : 'GET',
update : 'PUT',
destroy: 'DELETE'
},

api: {
create : 'create',
read : 'read',
update : 'update',
destroy: 'destroy'
},


buildUrl: function(request) {
var records = request.operation.records || [],
record = records[0],
format = this.format,
url = request.url || this.url;

if (this.appendId && record) {
if (!url.match(/\/$/)) {
url += '/';
}

url += record.getId();
}

if (format) {
if (!url.match(/\.$/)) {
url += '.';
}

url += format;
}

request.url = url;

return Ext.data.RestProxy.superclass.buildUrl.apply(this, arguments);
}
});

MWagner
11 Feb 2011, 2:27 PM
Just wanted to bump it up. Still have no clue how to fix the RestProxy. Maybe someone else is experienced enough to know where the error could be?

tof
13 Feb 2011, 4:46 AM
It seems this could be a work around (look for the // HERE comments) :


buildUrl: function(request) {
var records = request.operation.records || [],
record = records[0],
format = this.format,
url = request.url || this.url,
id = record ? record.getId() : request.operation.id; // HERE

if (this.appendId && id) { // HERE
if (!url.match(/\/$/)) {
url += '/';
}

url += id; // AND HERE
}

if (format) {
if (!url.match(/\.$/)) {
url += '.';
}

url += format;
}

request.url = url;

return Ext.data.RestProxy.superclass.buildUrl.apply(this, arguments);
}

MWagner
14 Feb 2011, 12:44 AM
Hi Christophe, thanks for your help! Now the GET param is appended to the url. But the success-function's param is undefined. :-?

The response of my rest-api gives me the correct result. But poi isn't filled with any data.



// POI List
app.poiListPanel = new Ext.List({
id : 'poiList',
scroll : 'vertical',
itemTpl : tplItem,
store : stores.pois.load(),
grouped : true,
onItemDisclosure: function(record) {
Ext.ModelMgr.getModel('Pois').load(record.data.id, {
success: function(poi) {
console.log(poi); // undefined
//app.poiDetailPanel.update(poi.data);
//app.poiDetailMediaPanel.update(poi.data);
app.Viewport.setActiveItem('poidetailpanel');
}
});
}
});

tof
14 Feb 2011, 12:51 AM
Humm, it's working for me...
Which version of sencha are you using ?

MWagner
14 Feb 2011, 12:54 AM
Latest Sencha Touch (1.0.1a).

MWagner
14 Feb 2011, 6:59 AM
Maybe it was a cache thing or something like that. Suddenly it works like it should.

Thanks for support! Helped me a lot!

tof
15 Feb 2011, 12:24 AM
Happy to know it ;)

I hope this will be fixed soon.

Trixor
7 Mar 2011, 5:20 PM
Also hoping this get fixed.

The Model is cool, but you can't do a clean load without patching proxy functionality. Also it doesn't do anything for ajax, would be nice if it appended ?id=### or &id=### to the url.

Thanks!

MWagner
8 Mar 2011, 12:04 AM
CRUD means you have URLs like /items/update/1 or /items/delete/1. Don't know why you want to append additional params. You should transport these params (apikey, etc.) in the header. ;)

So I hope they'll fix the basics first. :)

tthai
2 Nov 2011, 9:09 PM
Maybe it was a cache thing or something like that. Suddenly it works like it should.

Thanks for support! Helped me a lot!


I tried and this fixes the request to append id, but my record is undefined just like you initially reported. Any idea how you fixed it?

willv2
8 Jan 2015, 2:26 AM
I tried and this fixes the request to append id, but my record is undefined just like you initially reported. Any idea how you fixed it? tof (& now I) fixed it by recieving the Id from the operation not the record. You can pass data into the operation by passing an object into the load request. i.e:

store.load({id:5})
You'll also need to be using tof's fix.