PDA

View Full Version : model.save() and the required server response



manilodisan
10 Dec 2012, 1:38 PM
I have a model with a rest proxy (json reader/writer) defined. The json reader has the 'totalProperty' set to 'pagination.total_entries' as per my internal setup using rails 3 and 'will_paginate' gem.

Everything works fine when I query the records to show them in a grid but when I try to save a new model instance I get the 'failure' callback hit instead of success even though I respond with the 'success: true' message along with the newly created object.

Digging the failure operation param I get the following message: 'error: "Cannot read property 'total_entries' of undefined"' which means that the save operation looks for the pagination object (totalProperty) in the response. If I add a dummy pagination.total_entries = 1 in the response the success callback is hit but it seems pretty weird for extjs to seek for 'totalProperty' when I'm saving a record.

Is this expected behavior and I'm supposed to respond with this dummy in all my create actions?

Why is extjs expecting a collection-type response when I'm working with a single record (POST/PUT)?

flanders
10 Dec 2012, 2:28 PM
Normally you shouldn't have to provide the total property on a save. At least I never do and it works fine. ;)

Hunch: What might be the case is that standard the reader searches for the total property and fails silently if it cannot find it. However in this case it is trying to traverse nested JSON that doesn't exist and it doesnt get to failed silently on a undefined property, since it is trying to dereference an undefined before that.

What happens if you specify just an empty object pagination in the json result?

manilodisan
10 Dec 2012, 4:24 PM
What happens if you specify just an empty object pagination in the json result?

It works but it's a dumb approach to force the server to send such data just for extjs to play nice. Why should the reader look for a collection (total) when I'm posting a record. I'm using 4.1* by the way.

flanders
11 Dec 2012, 12:56 AM
I agree with you, that is dumb. However, I do not think that ExtJS is trying to force you to send that empty object. I think its just a bug (or actually more like a flaw) and think you should write a bugreport.

It was just a test to confirm my suspicion ;)