Ajax complete model post with jquery 1.6

426 views
Skip to first unread message

tome

unread,
Sep 15, 2011, 6:21:57 AM9/15/11
to play-framework
Hi,

I have a model with 12 fields. I'm doing an ajax post to my action
using similar to below:

$.ajax(
{
type:"post",
url: "@{Application.save()}",
data:{"user.fname" : "henry", "user.lname" : "bolwer" }
}
);

Action is standard action, e.g.:

public static void save(@Valid User user) {
...
}

This is fine, but is there a way to pass the whole user object and not
have to pass each individual field?

Ronald Haring

unread,
Sep 15, 2011, 7:53:07 AM9/15/11
to play-fr...@googlegroups.com
I would use ajax.form for this. So you define a normal form with all the fields in it and then attach the ajaxForm function to it, e.g.:

$('#multiplefields-form').ajaxForm({
            // dataType identifies the expected content type of the server response
            dataType:  'json',

            // success identifies the function to invoke when the server response
            // has been received
            success:    processJson
        });

This would submit the complete form and thus your controller will get a complete object

Regards
Ronald

tome

unread,
Sep 15, 2011, 8:46:07 AM9/15/11
to play-framework
Thanks, ajaxForm is exactly what I needed.

tome

unread,
Sep 15, 2011, 10:05:36 AM9/15/11
to play-framework
hmmm...maybe I spoke to soon.

The form does get submitted, however the data is not updated in the
model to reflect user changes. e.g. if form loads with fname="henry"
and is changed to fname="frank", on submit I can see in the POST that
fname="frank" is sent, however in the controller user.fname="henry".

Ronald Haring

unread,
Sep 15, 2011, 10:14:22 AM9/15/11
to play-fr...@googlegroups.com
Then I need to see the code that is repsonsible for this. I suppose that the complete name of the user's firstname in the html is user.fname? so:
<input type="text" name="user.fname" id="user_fname"/>

Alternatively what do you get when you submit the form without ajax? Same results?

Gr
Ronald

aoemaster

unread,
Sep 15, 2011, 10:27:44 AM9/15/11
to play-fr...@googlegroups.com
what about the serialize function of jquery ?


$.ajax(
   {
  type:"post",
  url: "@{Application.save()}",
  data: $('#myForm').serialize()
     }
);

Matt Hildebrand

unread,
Sep 15, 2011, 10:53:41 AM9/15/11
to play-fr...@googlegroups.com
One way is to submit a JSON representation of the object and let the server parse it into an object:

var request = {
  firstName: "Henry",
  lastName: "Bolwer"
};

$.ajax(
    {
        type: "POST",
        url: "@{Application.save()}",
        contentType: "application/json",
        data: JSON.stringify(request)
    }
);

On the server side, the JSON -> object conversion can often be a one-liner using a JSON library.

-Matt



--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.


Ronald Haring

unread,
Sep 15, 2011, 10:56:28 AM9/15/11
to play-fr...@googlegroups.com
seems like a lot of work for something that should work :)

My guess is that there is some kind of error on the html since I am using the same technique on some screens that I have build

Gr
Ronald

tome

unread,
Sep 15, 2011, 12:12:38 PM9/15/11
to play-framework
Appreciate your help Ronald and you are correct about an error on HTML
side. I had been wrestling with getting client side validation
working and had removed the object qualifier from the fields, so
instead of user.name (which I previously had) it was just name. So
this is working now, I have ajaxSubmit working correctly now.

d33t

unread,
Sep 16, 2011, 6:08:37 AM9/16/11
to play-framework
You can also take a look at this JS framework http://documentcloud.github.com/backbone/
.
I just found it in another thread today and haven't tested it, but
looks promising...

Regards
d33t

green

unread,
Sep 16, 2011, 7:09:24 AM9/16/11
to play-fr...@googlegroups.com
This is another cool stuff deserve a try: http://knockoutjs.com/

Marc Deschamps

unread,
Sep 16, 2011, 7:53:16 AM9/16/11
to play-fr...@googlegroups.com
+1 for knockout!

Work great with play+ajax / #jsAction

green

unread,
Sep 16, 2011, 8:43:50 AM9/16/11
to play-fr...@googlegroups.com
Hi Marc,

When I use KO with Play, the biggest concern is how to transfer model objects between Front and Back. 

From back to front, I will need to help of KO Mapping plugin, and I will have to process the mapped result to add some other attributes and methods, e.g. 

        $.extend(data, {
          showDetails: KO(false),
          toggleShowDetails: function() {data.showDetails(!data.showDetails());},
          title: KOD(function(){return 'State: ' + data.state().toLowerCase();})
        });

From front to back, it's a bit painful because Play binding mechanism does not support notation like a[b] (it does support a.b notation). You will need to either create a @Before methods and write your binding logic or create the model from JSON.

There are other issues like not all models are easily serialized to JSON, some properties I want to hide from the JSON presentation, it needs to explicitly pass JsonSerializer to renderJSON method which is tedious, etc. 

May I know how you were handliong these issues?

Thanks,
Green

On Fri, Sep 16, 2011 at 9:53 PM, Marc Deschamps <md.f...@gmail.com> wrote:
+1 for knockout!

Work great with play+ajax / #jsAction

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/PSE7sIuKKE8J.

Morten Kjetland

unread,
Sep 16, 2011, 1:31:50 PM9/16/11
to play-fr...@googlegroups.com

The problem with binding and jquery serializing is described here https://play.lighthouseapp.com/projects/57987/tickets/331-pojo-binding-does-not-work-with-jquery-ajax-serialisation

The ticket has link to a new binding impl which is not yet added to main play source. It would be nice if you tried out this new binding impl and sees if it helps you.

-morten

Reply all
Reply to author
Forward
0 new messages