tags:

views:

46

answers:

1

I'm using jQuery Stars Rating plugin ( http://www.fyneworks.com/jquery/star-rating/ ).

I'm having difficulty displaying an existing rating using the plugin's API. Everything works unless there is an existing rating in the database. If there is one, the following block of code will execute, and a 10 new ratings (that's as many inputs I have in my form) get sent to the back-end rating function.

<cfif aRating.recordCount>
    <script type="text/javascript"> 
        $(document).ready(function() {
            $('input').rating('select','#aRating.rating#');
        });
    </script>
</cfif>

This ajax call should ONLY execute when a user clicks on the stars

// Ajax submit rating
$('.star').rating({
    callback: function(value, link){
        $.ajax({
            type: "POST",
            url: "#URLFor(controller="membros", action="rateGame", key=gameProfile.id)#",
            data: "rating="+value,
            dataType: "json",
            success: function(response) {
            }
        });
    }
});

The form

<form class="rate-game">
    <input type="radio" name="star" class="star {split:2}" value="0.5">
    <input type="radio" name="star" class="star {split:2}" value="1">
    ... until 10
</form>

Any ideas what's causing the function to fire 10 times? If I remove the document.ready function, nothing happens, and the existing rating is not selected.

+2  A: 

The problem is that, well, the plugin API is weird. When you call:

$('input').rating('select', xxx);

it does the same thing for each input element. One of the things it does is invoke your callback function! You can make it not do that by passing in an extra argument of "false" there:

$('input').rating('select', xxx, false);

The following is incorrect and left here as a historical tidbit:

Your jQuery selector:

$(".star")

selects every one of your radio buttons and runs the code for each one. If you mean to do it only for the currently-selected button, try

$(".star:checked").rating({ ... })
Pointy
Pointy, thanks, but I think I did not explain myself very well: I only want the ajax to fire when someone clicks on the stars. The reason for the function in the first block of code is only to select an existing rating and display it, not do anything else with it. The only time the ajax call should execute is when there is a mouse click on the stars...
Mel
Yes, I think I understand; however I'm finding the documentation for that plugin to be grossly inadequate, so I'm trying to figure out from the source what's going on. Still, the fact that you've got 10 inputs, that the selector will select all 10, and that your form is submitted 10 times, well ...
Pointy
It appears that the $('input').rating('select','#aRating.rating#'); API is causing the form to fire only when it's encapsulated with a document.ready function...
Mel
Well that's because if it's *not* in such a function, and the `<script>` block is at the top of your document, then the code will run *before your `<input>` elements are part of the page*. Therefore the `$('input')` selector will find nothing.
Pointy
I take that back... it has nothing to do with the document.ready function... this now happens if the API is placed underneath the ajax call.... document.ready function was also causing that to happen... I think the API selecting the form is casuing the ajax to fire... i should change the callback in the ajax call maybe>?
Mel
I think that the plugin always automatically attaches itself to all the radio buttons on the page; there does not seem to be a way to prevent that (short of modifying the plugin).
Pointy
Then what on earth is the point of the "selected" API ? I can't do this server side by attaching a bunch of if statements, but it would have been nice to have it done in jquery...
Mel
@Mel well it's hard for me to tell *what* that plugin is trying to do. It seems to have been written by somebody who knows what they're doing, so I doubt it's severely broken, but it's got to be some sort of mismatch between what it expects and something on your page. You might try the Firebug or IE8 debugger and trace through it, I guess.
Pointy
@Mel Yes I agree, it's pretty weird -- have you gotten it to work on a small test page? I'll give it a quick try.
Pointy
Unfortunately not. The plugin itself works well until I use that API... I'll keep trying!
Mel
Is there a way to submit the form without using the callback?
Mel
OK, I figured it out. Change this: `$('input').rating('select', #foo#, false);` -- in other words, add another parameter to when you call it to set the selected star.
Pointy
That's a winner! Many thanks for your help, I appreciate it. Should add an answer to that effect.
Mel
I updated my original answer. It took a while of studying that source code and tinkering with a test program to figure it out.
Pointy