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/09 15:37:37 UTC

Ajax and LoadableDetachableModel

Quick question regarding a blog engine I'm working on: my user is editing a
blog Post and gets to the "Category" section.

Category is implemented with a DropDownChoice and LoadableDetachableModel.
For convenience - I also have a simple TextField that, when filled out,
implicitly creates a new Category and attaches it to the current Post. For
now, I only allow 1 Category per Post. The Category TextField takes
precedence over the DropDown. IE: if the user types anything in the Category
TextField, that (String) will be looked up, inserted if missing and attached
to the Post as a Category.

Question: after submitting this entire form (ala AjaxFallbackButton) - I
intentionally stay on this page. The Category is added and the Post has been
saved ... but I need the Category DropDown to refresh. Since all of this
happens ala AjaxFallbackButtons - the process works except, the
LoadableDetachableModel attached to the DropDownChoice (obviously) doesn't
fire the load method when I add the DropDown to the target (in the submit
handler).

To clarify, in my submission, I just added a Category and saved the Post and
ostensibly, redrawn the DropDownChoice (ie: I've set the OutputMarkupID on
the DropDownChoice and attached it to the target in the ajax submit handler)
... but the underlying model isn't actually reloading. Is there something
like a 'refresh' method on the LoadableDetachableModel ... or, should I
attach it to the target as well?

Thanks in advance for any suggestions,

-Luther

Re: Ajax and LoadableDetachableModel

Posted by Luther Baker <lu...@gmail.com>.
To help others: while debugging ... I noticed that the model's 'load' method
is being called before the button's onSubmit handler is invoked. So, under
normal circumstances, when control reaches the submit handler and I save the
value from the Textfield to the database as a 'Category' and add the
DropDownChoice to the target ... it is to no avail, the model's load handler
is not invoked again.

But, per Linda's suggestion, explicitly detaching the 'categoriesModel' just
before adding it to the target causes the model's 'load' method to get
invoked before rending to the view:

                protected void onSubmit(final AjaxRequestTarget target,
                                        final Form<?> form)
                {
                    Post updatedPost = (Post) form.getModelObject();

                    // new category? add to database and set in this post
                    updateCategoryInDatabase(updatedPost,
categoryCandidate.getModelObject());

                    // update author
                    final User user = ((UserSession)
Session.get()).getUser();
                    updatedPost.setAuthor(user);

                    // save post
                    final Post postWithId = postService.save(updatedPost);

                    // update the form's model (with the ID if it was saved
for the first time)
                    updatedPost = (Post) form.getModelObject();
                    updatedPost.setId(postWithId.getId());

                    // tell the world
                    final String msg = getTimestampString("m-saved");
                    info(msg);

*                    // redraw the categories
                    categoryModel.detach();
                    target.addComponent(categories);
*
                    // render the post's id (if not already rendered)
                    target.addComponent(postId);

                    // render the feedback message
                    target.addComponent(feedbackPanel);
                }


I hope this is the generally accepted way to do this. Thanks,

-Luther



On Tue, Jun 9, 2009 at 11:57 AM, Luther Baker <lu...@gmail.com> wrote:

> In my submit handler - I invoke a service to first a) the save category and
> then b) save the post.
>
> I put a message into the feedback panel and add the feedback panel and the
> drop down to the target.
>
> It is off an AjaxFallbackbutton ... so, no, I don't think I've done
> anything to explicitly detach the model. Is it such that when the original
> page is initially rendered - the model would have attached and then
> detached. The user then takes time to fill out the form - and upon submit,
> the model attaches again where I currently get the form fields ... do some
> *work* ... and then when I'm finished, detaches again?
>
> New ground for me here ... should I explicitly invoke detach() at the
> beginning of the submit handler ... and then attach() at the end so that the
> render phase gets new data?
>
> Thanks,
>
> -Luther
>
>
>
>
>
>
>
> On Tue, Jun 9, 2009 at 8:52 AM, Linda van der Pal <
> lvdpal@heritageagenturen.nl> wrote:
>
>> Did you explicitly detach the model?
>>
>> Regards,
>> Linda
>>
>> Luther Baker wrote:
>>
>>> Quick question regarding a blog engine I'm working on: my user is editing
>>> a
>>> blog Post and gets to the "Category" section.
>>>
>>> Category is implemented with a DropDownChoice and
>>> LoadableDetachableModel.
>>> For convenience - I also have a simple TextField that, when filled out,
>>> implicitly creates a new Category and attaches it to the current Post.
>>> For
>>> now, I only allow 1 Category per Post. The Category TextField takes
>>> precedence over the DropDown. IE: if the user types anything in the
>>> Category
>>> TextField, that (String) will be looked up, inserted if missing and
>>> attached
>>> to the Post as a Category.
>>>
>>> Question: after submitting this entire form (ala AjaxFallbackButton) - I
>>> intentionally stay on this page. The Category is added and the Post has
>>> been
>>> saved ... but I need the Category DropDown to refresh. Since all of this
>>> happens ala AjaxFallbackButtons - the process works except, the
>>> LoadableDetachableModel attached to the DropDownChoice (obviously)
>>> doesn't
>>> fire the load method when I add the DropDown to the target (in the submit
>>> handler).
>>>
>>> To clarify, in my submission, I just added a Category and saved the Post
>>> and
>>> ostensibly, redrawn the DropDownChoice (ie: I've set the OutputMarkupID
>>> on
>>> the DropDownChoice and attached it to the target in the ajax submit
>>> handler)
>>> ... but the underlying model isn't actually reloading. Is there something
>>> like a 'refresh' method on the LoadableDetachableModel ... or, should I
>>> attach it to the target as well?
>>>
>>> Thanks in advance for any suggestions,
>>>
>>> -Luther
>>>
>>>  ------------------------------------------------------------------------
>>>
>>>
>>> No virus found in this incoming message.
>>> Checked by AVG - www.avg.com Version: 8.5.339 / Virus Database:
>>> 270.12.59/2165 - Release Date: 06/09/09 05:53:00
>>>
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>

Re: Ajax and LoadableDetachableModel

Posted by Luther Baker <lu...@gmail.com>.
In my submit handler - I invoke a service to first a) the save category and
then b) save the post.

I put a message into the feedback panel and add the feedback panel and the
drop down to the target.

It is off an AjaxFallbackbutton ... so, no, I don't think I've done anything
to explicitly detach the model. Is it such that when the original page is
initially rendered - the model would have attached and then detached. The
user then takes time to fill out the form - and upon submit, the model
attaches again where I currently get the form fields ... do some *work* ...
and then when I'm finished, detaches again?

New ground for me here ... should I explicitly invoke detach() at the
beginning of the submit handler ... and then attach() at the end so that the
render phase gets new data?

Thanks,

-Luther






On Tue, Jun 9, 2009 at 8:52 AM, Linda van der Pal <
lvdpal@heritageagenturen.nl> wrote:

> Did you explicitly detach the model?
>
> Regards,
> Linda
>
> Luther Baker wrote:
>
>> Quick question regarding a blog engine I'm working on: my user is editing
>> a
>> blog Post and gets to the "Category" section.
>>
>> Category is implemented with a DropDownChoice and LoadableDetachableModel.
>> For convenience - I also have a simple TextField that, when filled out,
>> implicitly creates a new Category and attaches it to the current Post. For
>> now, I only allow 1 Category per Post. The Category TextField takes
>> precedence over the DropDown. IE: if the user types anything in the
>> Category
>> TextField, that (String) will be looked up, inserted if missing and
>> attached
>> to the Post as a Category.
>>
>> Question: after submitting this entire form (ala AjaxFallbackButton) - I
>> intentionally stay on this page. The Category is added and the Post has
>> been
>> saved ... but I need the Category DropDown to refresh. Since all of this
>> happens ala AjaxFallbackButtons - the process works except, the
>> LoadableDetachableModel attached to the DropDownChoice (obviously) doesn't
>> fire the load method when I add the DropDown to the target (in the submit
>> handler).
>>
>> To clarify, in my submission, I just added a Category and saved the Post
>> and
>> ostensibly, redrawn the DropDownChoice (ie: I've set the OutputMarkupID on
>> the DropDownChoice and attached it to the target in the ajax submit
>> handler)
>> ... but the underlying model isn't actually reloading. Is there something
>> like a 'refresh' method on the LoadableDetachableModel ... or, should I
>> attach it to the target as well?
>>
>> Thanks in advance for any suggestions,
>>
>> -Luther
>>
>>  ------------------------------------------------------------------------
>>
>>
>> No virus found in this incoming message.
>> Checked by AVG - www.avg.com Version: 8.5.339 / Virus Database:
>> 270.12.59/2165 - Release Date: 06/09/09 05:53:00
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Ajax and LoadableDetachableModel

Posted by Linda van der Pal <lv...@heritageagenturen.nl>.
Did you explicitly detach the model?

Regards,
Linda

Luther Baker wrote:
> Quick question regarding a blog engine I'm working on: my user is editing a
> blog Post and gets to the "Category" section.
>
> Category is implemented with a DropDownChoice and LoadableDetachableModel.
> For convenience - I also have a simple TextField that, when filled out,
> implicitly creates a new Category and attaches it to the current Post. For
> now, I only allow 1 Category per Post. The Category TextField takes
> precedence over the DropDown. IE: if the user types anything in the Category
> TextField, that (String) will be looked up, inserted if missing and attached
> to the Post as a Category.
>
> Question: after submitting this entire form (ala AjaxFallbackButton) - I
> intentionally stay on this page. The Category is added and the Post has been
> saved ... but I need the Category DropDown to refresh. Since all of this
> happens ala AjaxFallbackButtons - the process works except, the
> LoadableDetachableModel attached to the DropDownChoice (obviously) doesn't
> fire the load method when I add the DropDown to the target (in the submit
> handler).
>
> To clarify, in my submission, I just added a Category and saved the Post and
> ostensibly, redrawn the DropDownChoice (ie: I've set the OutputMarkupID on
> the DropDownChoice and attached it to the target in the ajax submit handler)
> ... but the underlying model isn't actually reloading. Is there something
> like a 'refresh' method on the LoadableDetachableModel ... or, should I
> attach it to the target as well?
>
> Thanks in advance for any suggestions,
>
> -Luther
>
>   
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com 
> Version: 8.5.339 / Virus Database: 270.12.59/2165 - Release Date: 06/09/09 05:53:00
>
>   


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