2

I'm displaying feedback and rating for a particular entity which is shown as tab initially. When the user clicks a tab, I am getting feedback and rating using

$.get()

The return of $.get() is something like this

some arbit html|||8

This data I'm splitting using |||and getting the rating which is 8 in this case.

        $.get('prevfeed.php',{text:text},function(data){
         data = data.split("|||");

        $('#prevFeedback').html(data[0]);
        $('body').data( 'rating', parseInt(data[1]));
        alert(parseInt(data[1])+1);
    });

The problem is here. I am using this plugin to show star rating. The following is the initializing code

        $('#star').raty({
        readOnly: true,
        number : 10,
        start:   $('body').data( 'rating')
    }); 

Whatever i do The rating does not work. If i put a number instead of $('body').data( 'rating'), it's working fine.

Initially I tried setting a value of hidden input field using $('body').data( 'rating')and then assigning start with the val of the input field. But it's of no use.

Thanks in advance

P.S: Seeing the answers I edited my code

        $('#star').raty({
            readOnly: true,
            number: 10,
            start: function() { alert($('body').data('rating'););return $('body').data('rating'); }

When I include alert, it is working. So it is like I just have to deplay this code. How to do this. });

Krishna Deepak
  • 1,735
  • 2
  • 20
  • 31
  • If you replace `parseInt(data[1])+1` in your `alert` with `$('body').data('rating')`, do you get the right thing in the alert? – Mark Reed Apr 25 '12 at 10:23
  • Are you certain the 'rating' data has been set by the time the plugin is initialized? This might be a matter of functions not firing when you expect them to, so the raty plugin might be getting initialized (with an undefined value) before the bits that define what data is. – shu zOMG chen Apr 25 '12 at 10:27

2 Answers2

4

This is because the code where you set the rating attribute on the body is delayed until the AJAX call has completed.

However, you'll be initiating the raty plugin on page load; where the rating data attribute is still undefined.

You'll have to delay the initialization of the plugin until the AJAX call has completed:

$.get('prevfeed.php', {
    text: text
}, function(data) {
    data = data.split("|||");

    $('#prevFeedback').html(data[0]);
    $('body').data('rating', parseInt(data[1]));

    $('#star').raty({
        readOnly: true,
        number: 10,
        start: $('body').data('rating')
    });
});​
Matt
  • 74,352
  • 26
  • 153
  • 180
  • 1
    I was about to write the same solution :P – Rakesh Juyal Apr 25 '12 at 10:30
  • Just to add to that, depending on what you (@Napster) want to do, you can initialise raty with 0 and use @Matt 's solution to update when the right value is ready :) – DiogoNeves Apr 25 '12 at 10:35
  • now im getting this error like $("#star").raty is not a function for the above code – Krishna Deepak Apr 25 '12 at 12:38
  • `$("#star").raty is not a function` means the plugin is installed. Check the method is called `raty`, check `$ === jQuery` and check that the `` tag which adds the `raty` plugin points to the right place and loads successfully. – Matt Apr 25 '12 at 15:49
0

The solution which Matt gave should work. Another solution can be, On clicking the tab as of now you are fetching the data from prevfeed.php and trying to set the rating accordingly.

You can try this instead.

On clicking the tab:

$("#star").raty ( {
readonly: true,
number: 10,
start: function (){
//get the rating now,
$.get('prevfeed.php',{text:text},function(data){
        data = data.split("|||");

        $('#prevFeedback').html(data[0]);
        $('body').data( 'rating', parseInt(data[1]));
    });
//Now you got the rating, just return it
return $('body').data('rating');
}
}
);

P.S. I haven't tried it, but it should work ;)

Rakesh Juyal
  • 35,919
  • 68
  • 173
  • 214
  • this is not working either. while i'm getting error with the above answer which I have commented about. This answer does not return any error but still the rating does not work – Krishna Deepak Apr 25 '12 at 12:46