PDA

View Full Version : Update model with data from server



bgartenmann
27 May 2011, 5:13 AM
Hi all :)

I have a short question to which I couldn't find a solution on the forum.
Is it possible to update a model instance with data from a server (JSON)?

So, for example I have a user model with a has_many association to a model appointment.
Now I load a store with many user models, but not all information on the users, e.g. only first- and lastname.
JSON response would look something like this:

[{
"id": "1",
"firstname": "Test1",
"lastname": "User1"
},
{
"id": "2",
"firstname": "Test2",
"lastname": "User2"
},
{
"id": "3",
"firstname": "Test3",
"lastname": "User3"
}]

After a click on a user I want to load and show the details of this user including all appointments through the association.
JSON response:

{
"id":"1",
"firstname":"Test1",
"lastname":"User1",
"appointments":[
{
"id": "1",
"title":"appointment1"
},
{
"id": "2",
"title": "appointment2"
}
]
}

How can I update the user instance in the store with this new data?

Hgopal
28 May 2011, 2:41 AM
Hi

I am very new to Sencha Touch, I have similar situation of update model with data from server.

I have created Model like

Ext.regModel('User_Role', {
fields: [
{ name: 'User_Role_Id', type: 'int' },
{ name: 'Name', type: 'string' }

]
});

Want to fill this model from the data return from Server ( which is in JSON format)


function GetKeystoneUserRoleList() {
var baseUrl = "/Services/MasterService.svc/GetUserRoleList"; // Service url
Ext.Ajax.request({
url: baseUrl,
method: "POST",
model: 'User_Role',
success: function(result, request) {
var UserRole = result.responseText;
alert(result.responseText);
var resp = Ext.decode(result.responseText);

// alert(resp[1].Name);
// alert(resp.length);

var html = tpl.applyTemplate(resp);
Ext.getCmp('content').update(html);
},
failure: function(error) {
alert(error.status + " - " + error.statusText + error.message);
}

}) //End of Ajax Call

} //End

Note: My Code may wrong.

bgartenmann
30 May 2011, 4:46 AM
Hi Hgopal

If your JSON response is a list of models, the easiest way is with a store.
e.g.


Ext.regModel('User_Role', {
fields: [
{ name: 'User_Role_Id', type: 'int' },
{ name: 'Name', type: 'string' }
]
});

var myStore = new Ext.data.Store({
model: 'User_Role',
proxy: {
type: 'ajax',
url : '/Services/MasterService.svc/GetUserRoleList',
reader: {
type: 'json',
root: 'users' //or whatever your root is
}
}
});

myStore.load(); //this should fill your user_role models with the data from the server


To work with this example, your JSON response should be formatted like this:


{
"users": [
{
"id": 1,
"name": "user role 1",
},
{
"id": 2,
"name": "user role 2",
}
]
}


Hope this helps!

Happy coding ;)

P.S for potential readers: my problem described in the initial post still remains, because I'm able to fill my store with data from server, but I would like to update one model instance in the store with new/extended data from the server...
According to the docs this should be possible with Model.load, but unfortunately this doesn't seem to work... see also this thread: http://www.sencha.com/forum/showthread.php?122595-Model.load-not-sending-id-loading-all-models

Does anyone have another solution or probably a fix for the Model.load method?

Hgopal
1 Jun 2011, 1:00 AM
Thanks a lot. I found the mistake is in my JSON format. I got JSON result from our Web services as below (Without Root - User).

[
{
"id": 1,
"name": "user role 1",
},
{
"id": 2,
"name": "user role 2",
}
]
Now I have made changes in my Web service to return JSON result with root details. Thanks.