You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Luther Baker <lu...@gmail.com> on 2009/06/07 16:32:21 UTC

Ajax Buttons and Form

I'm saving a 'Post', like a blog post, and JPA's merge function doesn't
update the object in place but rather, returns an object reflecting the new
database fields (like "id" if it were set).

In my application, I'd like to manage insert / update by setting this new
'Post' as the model for my form. In this case, Updates have ids ... but the
following gives me grief since the Form<?> is not typed. I can't cast to
IModel<?> - so not sure of the right thing to do here. Thoughts?


Here is the form field:

            final TextField<String> *postId* = new TextField<String>("id");
            postId.setOutputMarkupId(true);
            add(postId);


Here is my save handler:

                @Override
                protected void onSubmit(final AjaxRequestTarget target,
                                        final Form<?> form)
                {
                    ...
// works fine
                    post = (Post) form.getModelObject();
                    final Post postWithId = postService.save(post);
// works fine
                    final IModel<Post> newModel = new
CompoundPropertyModel<Post>(postWithId);

*// 1. I want to use this new 'Post' object but neither of these compile*
                    form.setModel(newModel);
                    form.setModelObject(postWithId);

ERROR: The method setModelObject(capture#6-of ?) in the type
Form<capture#6-of ?> is not applicable for the arguments (Post)



*2. This compiles fine but doesn't actually update the id field on screen:*

                    form.setDefaultModelObject(postWithId);
                    target.addComponent(*postId*);



*3. Or, instead resetting the model, I can manually set the id field on the
existing model - which is all I really want to accomplish in this case:
*
                    post = (Post) form.getModelObject();
                    final Post postWithId = postService.save(post);
                    post.setId(postWithId.getId());

                    feedbackPanel.add(new SimpleAttributeModifier("class",
"info"));
                    info(getString("m-successfully-saved"));

                    target.addComponent(feedbackPanel);
                    target.addComponent(postId);

This seems to work, updates the id on screen and causes subsequent JPA
'merge' invocations to update instead of insert. As you can see, being an
AJAX button, I did have to explicitly render the new id field .

So, is there a well-defined approach for this sort of thing?

1 - is there a way I can actually reset the model?
2 - when I reset the DefaultModelObject - the the id isn't rendering to
screen - which implies I don't know what the DefaultModelObject actually is.
3 - Is it appropriate to use the Form fields to help discern between updates
and inserts - or is there a more appropriate way to manage this within
Wicket that I'm missing. My next step is to HIDE the id on the screen - but
wanted to make sure that it was indeed a well-received way to even manage
this type of update.

Thanks,

-Luther

Re: Ajax Buttons and Form

Posted by Warren Bell <wa...@gmail.com>.
Add the Model to the form and override IModel#getObject() return updated 
new "Post" object. I read on this mailing list not to set Models.

An example with a LoadableDetachableModel.

            Form blogPostForm = new Form(id, new 
CompoundPropertyModel<Post>(new LoadableDetachableModel<Post>()
            {
 
                @Override
                protected Post load()
                {
                    return postService.save(post); 
                }
               
            }));

Warren

Luther Baker wrote:
> I'm saving a 'Post', like a blog post, and JPA's merge function doesn't
> update the object in place but rather, returns an object reflecting the new
> database fields (like "id" if it were set).
>
> In my application, I'd like to manage insert / update by setting this new
> 'Post' as the model for my form. In this case, Updates have ids ... but the
> following gives me grief since the Form<?> is not typed. I can't cast to
> IModel<?> - so not sure of the right thing to do here. Thoughts?
>
>
> Here is the form field:
>
>             final TextField<String> *postId* = new TextField<String>("id");
>             postId.setOutputMarkupId(true);
>             add(postId);
>
>
> Here is my save handler:
>
>                 @Override
>                 protected void onSubmit(final AjaxRequestTarget target,
>                                         final Form<?> form)
>                 {
>                     ...
> // works fine
>                     post = (Post) form.getModelObject();
>                     final Post postWithId = postService.save(post);
> // works fine
>                     final IModel<Post> newModel = new
> CompoundPropertyModel<Post>(postWithId);
>
> *// 1. I want to use this new 'Post' object but neither of these compile*
>                     form.setModel(newModel);
>                     form.setModelObject(postWithId);
>
> ERROR: The method setModelObject(capture#6-of ?) in the type
> Form<capture#6-of ?> is not applicable for the arguments (Post)
>
>
>
> *2. This compiles fine but doesn't actually update the id field on screen:*
>
>                     form.setDefaultModelObject(postWithId);
>                     target.addComponent(*postId*);
>
>
>
> *3. Or, instead resetting the model, I can manually set the id field on the
> existing model - which is all I really want to accomplish in this case:
> *
>                     post = (Post) form.getModelObject();
>                     final Post postWithId = postService.save(post);
>                     post.setId(postWithId.getId());
>
>                     feedbackPanel.add(new SimpleAttributeModifier("class",
> "info"));
>                     info(getString("m-successfully-saved"));
>
>                     target.addComponent(feedbackPanel);
>                     target.addComponent(postId);
>
> This seems to work, updates the id on screen and causes subsequent JPA
> 'merge' invocations to update instead of insert. As you can see, being an
> AJAX button, I did have to explicitly render the new id field .
>
> So, is there a well-defined approach for this sort of thing?
>
> 1 - is there a way I can actually reset the model?
> 2 - when I reset the DefaultModelObject - the the id isn't rendering to
> screen - which implies I don't know what the DefaultModelObject actually is.
> 3 - Is it appropriate to use the Form fields to help discern between updates
> and inserts - or is there a more appropriate way to manage this within
> Wicket that I'm missing. My next step is to HIDE the id on the screen - but
> wanted to make sure that it was indeed a well-received way to even manage
> this type of update.
>
> Thanks,
>
> -Luther
>


-- 
Thanks,

Warren Bell
909-645-8864
warrenbell2@gmail.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org