PDA

View Full Version : Save model passing itself to the proxy



nextSTEP
28 Sep 2011, 5:50 AM
Hi,

given the following model:


Ext.define("User", {
extend : "Ext.data.Model",

fields : [
{ name : "id" },
{ name : "name" }
],
proxy : {
type : "rest",
api : {
create : "/User/Create",
read : "/User/Get",
update : "/User/Save",
destroy : "/User/Delete"
}
}
});

Updating a model instance calling user.save(); causes me to specify following method parameters on server side:

UserController.Save(int id, string name);

What if the model has much more fields? Is there a way to pass the model itself as the sole parameter instead of its fields?

Target:
UserController.Save(User user);

nextSTEP
29 Sep 2011, 12:59 AM
Figured it out!

Set this writer config on the proxy:

writer : {
encode : true,
root : "user" // Param name
}

which causes the Ajax request to send -> data : "user=[encoded object]"

So this works now:

UserController.Save(User user);

nextSTEP
29 Sep 2011, 2:23 AM
This works partially.. I just get the JSON string, not the decoded User instance.

So I googled a lot and just removed the writer config and declared the Server-method with the User-object parameter. This works now.
Really stupid that I didn't try this from the beginning.