eddysilvamendes / jquery-star-rating-plugin

Automatically exported from code.google.com/p/jquery-star-rating-plugin
0 stars 0 forks source link

Problem integrating autosubmit and dynamically set rating value #27

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Thanks for this plugin.

I'm having trouble making the plugin auto postback and at the same time
dynamically selecting the value. Here is what I have. 

<script type="text/javascript" charset="utf-8">
$(':radio.star').rating({ callback: function(value, link) {
  $.ajax({
    type: "POST",
    url: "description.aspx/newScore",
    data: "{'score':'" + value + "','place':'" + $("#title").html() + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
       $(prefix + "LRating").html(msg.d);
    }
  });
 }
});

$(document).ready(function() {
      $(":radio.star").rating('select',3);
});
</script>

This works, but on this code

$(document).ready(function() {
      $(":radio.star").rating('select',3);
});

It makes 5 postbacks to the server.

Any insight
Thanks

Original issue reported on code.google.com by roncan...@gmail.com on 21 May 2009 at 3:52

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
I think what your experiencing is a very similar issue to that which I had. 
Basically
you don't want to set the callback until after you have called select, however 
this
is not really possible (as far as I can tell, but I'm a jquery noob and 
definitely
don't consider javascript as a forte).

In anycase, I have attached a patch that can be applied to jquery.rating.js 
which
will add one method called setCallback.

So you can do the following:

=======8<=======
$(":input", "#votes-comment-{{ comment.id }}")
    .rating() // init, in case your using a custom class.
    .rating("select", "{{ score.star_rating }}") // select your value
    .rating("setCallback", myFunction) // set a callback
=======>8=======

This of course means that you can do the following:

=======8<=======
function(value, link) {
    var form = this.form;
      $.ajax({
        type: "POST",
        url: "description.aspx/newScore",
        data: "{'score':'" + value + "','place':'" + $("#title").html() + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg, score) {
            $(prefix + "LRating").html(msg.d);
            $(":input", "#" + $(form).attr('id'))
                .rating("setCallback", null)
                .rating("select", score + "")
                .rating("setCallback", vote);
        }
      });
     }
=======>8=======

The code attaches the click action to the select which is fine, however all I'm
trying to do here is run the select without actioning the callback. Perhaps it 
would
be better to have a param for the select call that specifies not to run the 
callback?

Original comment by alex%alu...@gtempaccount.com on 4 Aug 2009 at 1:57

Attachments:

GoogleCodeExporter commented 8 years ago
hello, i had the same problem than roncansan.

I called the "select" method into the "callback" method so it unfortunately did 
an
infinite loop, (because select method calls the callback method).

what i did is add the argument : "wantCallBack" to the function "select", as 
a...@alution.com suggest it. So i can avoid select method to do a callback.

it's seems to work as well as before on classic ratings. and now i can call the
"select" in the callback like in this example :

 $(document).ready(function() {
   $('.auto-ajax-starUPD').rating({
                callback: function(value, link){
                    $.ajax({//une fonction ajax qui recalcule le score via page PHP
                       type: "POST",
                       url: "getScore.php",// GET NEW AVERAGE VOTE BY AJAX
                       data: "name=John&location=Boston",
                       success: function(msg){
                         //alert( "Data Saved: score : " + msg );
                         $("#fajaxUPD > input").rating('select',msg,false);//UPDATE
SELECTED STAR USING THE MSG AJAX
                         $("#fajaxUPD > input").rating('readOnly', true)

                       }
                     });
                }
     }); 
});

$(selector).rating('select',msg,false);

where msg is the value I want to select
and false is the value for "wantCallBack"

attached file contains my version of select method, if you want try it, you 
have to
replace original method by this one.

tell me if it could be usuable for anyone.

thanks a lot !

Original comment by faivre.t...@gmail.com on 7 Dec 2009 at 12:59

Attachments:

GoogleCodeExporter commented 8 years ago
I had the exact same issue and with your patch it works like a charm.
Many thanks
RogeR

Original comment by rsch...@gmail.com on 26 Mar 2010 at 11:43

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Same here, the patch works great! Please commit the patch. 

Original comment by google%e...@gtempaccount.com on 26 Mar 2010 at 12:19

GoogleCodeExporter commented 8 years ago
Wow. Great work guys. Sorry I haven't been around.
I'm installing the patch now and it will be part of v3.13+ released later today.

Stay up-to-date by following us here:
http://twitter.com/fyneworks

Original comment by fyneworks on 26 Mar 2010 at 3:51