AJAX Rating extension, multiple on a page

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

AJAX Rating extension, multiple on a page

Daniel Watrous-2
Hi,

I've been working with the Rating extension found here:
http://www.wicket-library.com/wicket-examples/ajax/ratings?0

I have a case where I need to render multiple on a page, and they
render fine. Each rating panel that displays corresponds to a specific
record in a database. I would like to be able to get information from
the request to indicate which record the rating is for.

The RatingPanel as a method onRated that accepts the rating and runs
an update, like this:

                    @Override
                    public void onRated(int rating, AjaxRequestTarget target) {
                        HomePage.rating.addRating(rating);
                    }

I also noticed that the URL contains some additional information

<a href="?0-4.ILinkListener-movies-0-rating-rater-element-0-link"
wicket:id="link" id="linkbb" onclick="var
wcall=wicketAjaxGet(&#039;?0-4.IBehaviorListener.0-movies-0-rating-rater-element-0-link&#039;,function()
{ }.bind(this),function() { }.bind(this), function() {return
Wicket.$(&#039;linkbb&#039;) != null;}.bind(this));return
!wcall;"><img wicket:id="star"
src="wicket/resource/org.apache.wicket.extensions.rating.RatingPanel/star1-ver-1326919989539.gif"/></a>

The number between ...movies-_-rating... (where the underscore is) is
different for each group.

Is there some direct way to access information about the URL or to
modify this extension to pass the record information along with it?

Thanks,
Daniel

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: AJAX Rating extension, multiple on a page

armhold
I've not used this component before, so take this with a grain of salt.

But if you're overriding the RatingPanel.onRating(), you implicitly have access to the RatingPanel via "this". You can access it in the onRating() method, as well as any other accessible member/final field. So you could do something like:

final Object thingBeingRated = ...;

      @Override
      public void onRated(int rating, AjaxRequestTarget target) {
          HomePage.rating.addRating(rating);
          log.debug("thing being rated: " + thingBeingRated);
      }
Reply | Threaded
Open this post in threaded view
|

Re: AJAX Rating extension, multiple on a page

armhold
Just to be clear in case it wasn't obvious- thingBeingRated will be serialized with the rest of your page if you take this approach. If it's not serializable, use a reference (like a database ID) to look it up when needed, instead of marking the object itself as final.
Reply | Threaded
Open this post in threaded view
|

Re: AJAX Rating extension, multiple on a page

Daniel Watrous-2
It turned out to be easier than I thought (must not have been thinking
in the wicket way).

Here's what I do (notice that inside populateItem I have access to
movieItem, which I can use to getModelObject()):

        // Add movieListView of existing movies
        moviesForm.add(new PropertyListView<Movie>("movies", movieList) {

            @Override
            public void populateItem(final ListItem<Movie> movieItem) {
                final RatingModel rating = new
RatingModel(movieItem.getModelObject().getRating());
                movieItem.add(new
TextField<String>("name").setType(String.class));
                movieItem.add(new DropDownChoice<Category>("category",
Arrays.asList(Category.values()), new
EnumChoiceRenderer<Category>(this)));
                movieItem.add(new RatingPanel ("rating", new
PropertyModel<Integer>(rating, "rating"), 5, new
PropertyModel<Integer>(rating, "numberOfVotes"), false) {
                    @Override
                    public boolean onIsStarActive(int star) {
                        return rating.isActive(star);
                    }
                    @Override
                    public void onRated(int newRating,
AjaxRequestTarget target) {
                        movieItem.getModelObject().setRating(newRating);
                        rating.updateRating(newRating);

                        Session session =
HibernateUtil.getSessionFactory().getCurrentSession();
                        session.beginTransaction();
                        session.update(movieItem.getModelObject());
                        session.getTransaction().commit();

                        movieList.detach();
                    }
                });
                movieItem.add(new Link("removeLink") {
                    @Override
                    public void onClick() {
                        System.out.print(movieItem.getModelObject().getId());
                        Session session =
HibernateUtil.getSessionFactory().getCurrentSession();
                        session.beginTransaction();
                        session.delete(movieItem.getModelObject());
                        session.getTransaction().commit();
                        movieList.detach();
                    }
                });
            }
        }).setVersioned(false);

Daniel

On Sun, Jan 22, 2012 at 12:46 PM, armhold <[hidden email]> wrote:

> Just to be clear in case it wasn't obvious- thingBeingRated will be
> serialized with the rest of your page if you take this approach. If it's not
> serializable, use a reference (like a database ID) to look it up when
> needed, instead of marking the object itself as final.
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/AJAX-Rating-extension-multiple-on-a-page-tp4317346p4318956.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]