viezel / napp.alloy.adapter.restapi

RestAPI Sync Adapter for Titanium Alloy Framework
197 stars 102 forks source link

Fetching single model expects single element array rather than a plain object #42

Closed claudijo closed 10 years ago

claudijo commented 10 years ago

My assumption is that a REST endpoint for a identified resource should return a plain key-value object, for example: Request: GET /api/myResources/1234 Response: { id: '1234', name: 'whatever', ...}

But is seems that the alloy adapter expects the response with the single resource to be wrapped in an array, in order to parse the resource and create a backbone model, that is:

[ { id: '1234', name: 'whatever', ...} ]

viezel commented 10 years ago

yes. It should not be wrapped in an array. Single models are returned as an object. and the adapter is designet for it. I dont have any issues regarding this.. whats the issue?

joakimkanon commented 10 years ago

I think the issue is that if the API returns a collection, it works fine. But if it is a single object, it doesn't.

I believe this for loop is the problem. https://github.com/viezel/napp.alloy.adapter.restapi/blob/master/restapi.js#L256

If the API returns a model (or single object), the for loop loops through the object attributes instead of collection rows. So when @claudijo is putting the model in an array, it works as intended. ;)

viezel commented 10 years ago

true.. I can tell that the rest adapter is not as updated as the restsql. it has the following: https://github.com/viezel/napp.alloy.adapter.restsql/blob/master/sqlrest.js#L570 and isCollection helps define what to do.

You could use same structure and see it that works.. if so, please do a PR.