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. test2-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 ||
function(response){
container.children('.star-rating-result')
.html(response)
}
);
return false;
}
$(function (){
function starRater(res){
$('#vote1').empty().rater(
'./ratingsdemo.php', {
maxvalue : 5,
style : 'basic',
curvalue : res||0,
callback : starRater
});
}
starRater(0);//An initial curvalue is 0.
});
When you click. response ratingsdemo.php
<?php echo($ratingData); ?>(*Note the following check etc. when actually using it. isset($_POST['rating']) , is_numeric($_POST['rating']) length etc...)
$('#vote1').empty().rater(
'./ratingsdemo.php', {
maxvalue : 5,
style : 'basic',
curvalue : 3,
callback : starRater
});
$(function (){
function starRater(res){
$('#vote2').empty().rater(
'./ratingsdemo2.php', {
maxvalue : 5,
style : 'basic',
curvalue : res||0,
callback : function(resoj){
eval("var oj="+resoj);
starRater(oj.Average);
$('#vote2').children('.star-rating-result').html(
oj.message
+"<br>Your click is : "+oj.myrate
+"<br>Average is : "+oj.Average
);
}
});
}
starRater(0);
});
ratingsdemo2.php response
{
myrate : <?php echo($ratingData); ?>,
Average : 3, #for example result from SQL
message : "Hello rate!"
}
(*Note the following check etc. when actually using it. isset($_POST['rating']) ,
is_numeric($_POST['rating']) length etc...)