jQuery Ajax Rater Plugin is great work. I want 'callback' option for data from server. Is it useless?
* Valid options: * --------------------------------------- * style: 'basic', 'inline' OR 'small' * maxvalue: the maximum value / number of stars * curvalue: the initial value / selected stars * callback: function for data from server <-thisFor example, when a few lines are added to jquery.rater.js. test-settings.callback-jquery.rater.js
jQuery.fn.rater = function(url, options)
{
if(url == null) return;
var settings = {
url : url, // post changes to
maxvalue : 5, // max number of stars
curvalue : 0, // number of selected stars
callback : null //=========================== test-settings.callback
};
(Omission)
if(settings.maxvalue == 1) // on / off
{
settings.curvalue = (settings.curvalue == 0) ? 1 : 0;
jQuery(container).find('.star-rating').children('.current-rating')
.css({width:(settings.curvalue*100)+'%'});
jQuery.post(container.url, { "rating": settings.curvalue });
return false;
}
else
{
settings.curvalue = stars.index(this) + 1;
raterValue = jQuery(this).children('a')[0].href.split('#')[1];
jQuery.post(container.url, { "rating": raterValue },
//=====test-settings.callback
(settings.callback)?
settings.callback:
function(response){
container.children('.star-rating-result')
.html(response)
}
);
return false;
}
$(function (){
function responseCounter(res){
$('#vote1').empty().rater(
'./ratingsdemo.php', {
maxvalue : 5,
style : 'basic',
curvalue : res||0,
callback : responseCounter
});
}
responseCounter(0);//An initial curvalue is 0.
});
When you click. response ratingsdemo.php
<?php echo($_POST['rating']); ?>For instance,in this sample, if ( php's response == 3) is meaning settings.callback(3) .
$('#vote1').empty().rater(
'./ratingsdemo.php', {
maxvalue : 5,
style : 'basic',
curvalue : 3,
callback : responseCounter
});
$(function (){
function responseCounter(res){
$('#vote2').empty().rater(
'./ratingsdemo2.php', {
maxvalue : 5,
style : 'basic',
curvalue : res||0,
callback : function(resoj){
eval("var oj="+resoj);
responseCounter(oj.Average);
$('#vote2').children('.star-rating-result').html(
oj.message
+"<br>Your click is : "+oj.myrate
+"<br>Average is : "+oj.Average
);
}
});
}
responseCounter(0);
});
ratingsdemo2.php response
{
myrate : <?php echo($_POST['rating']); ?>,
Average : 3, #for example result from SQL
message : "Hello rate!"
}