PDA

View Full Version : [FIXED]Model.load response structure



sg707
28 Feb 2011, 12:03 PM
Let say I have the following RESTful URI

/users/0.json

This returns one user account w/ the id "0"

I return the following response



{
"users": {
"name":"Ed Spencer",
"id":0
}
}
but this does not work because it is expecting an "array" format like


{
"users": [
{
"name":"Ed Spencer",
"id":0
}
]
}
As it returns exactly "1" user info, I don't want this in the array format.

What's even funny is that I intentionally returned multiple records when invoking users/0.json


{
"users": [
{
"name":"Ed Spencer",
"id":0
},
{
"name":"John Lee",
"id":1
}

]
}
It only returns 1 record from the array which is "Ed Spencer" user account... So even ExtJS4 is reading only 1 record...


User.load('0', {
success: function(record, operations) {
console.log(record.get('name'));
}
});


Above code prints "Ed Spencer". So, I don't see any reason why it should be "array"

icflorescu
2 Mar 2011, 9:57 AM
In my tests the server response doesn't need a "data root", i.e. it's enough to have:


[
{
"name":"Ed Spencer",
"id":0
}
]

But you still need to put it in an array, which I agree, is unnecessary if you're only reading one entity.

There are still few things under development/bug fixing in Ext.data and since there are people anxious to start using ExtJS 4 in new projects maybe it would be a good idea to publish an ajax/json/RESTful format & conventions guide.

That would give people time to focus on their server-side until the beta/final release comes out... It's just a suggestion.

sg707
2 Mar 2011, 11:15 AM
I completely agree! Would be wonderful if they provide such guide...

This could all easily be solved if they give a sample Pet Shop application code that utilizes all of MVC features.

icflorescu
4 Mar 2011, 12:37 PM
I've noticed this has been addressed in dev preview 3.

Serving a response like:


{
"users": {
"name":"Ed Spencer",
"id":0
}
}

or simply:


{
"name":"Ed Spencer",
"id":0
}

to a Model.load() RESTful request now works perfectly.

Excellent work, dev team!

evant
13 Mar 2011, 7:56 PM
Indeed, it will now handle this case!