eddysilvamendes / jquery-star-rating-plugin

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

Rating plugin becomes inactive on rewriting an element #5

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Run attached html file in Mozilla/IE browser
2. Rating elemnts change and cancel option goes away
3. Clicking on rating doesnt work

What is the expected output? What do you see instead?
It should not throw an error and become inaffective.

What version of the product are you using? On what operating system?
jquery 1.3.1 and Star rating plugin 2.61

Please provide any additional information below.
Firebug shows following error:

elem.parentNode is null
children()()jquery.js (line 1259)
map()()jquery.js (line 1211)
contents()()jquery.js (line 1264)
(?)()()3DYR8yd%...e3Q%3D%3D (line 1)
(?)()()3DYR8yd%...e3Q%3D%3D (line 1)
handle()()jquery.js (line 2076)
(?)()()jquery.js (line 1858)
[Break on this error] siblings: function(elem){return
jQuery....ling(elem.parentNode.firstChild,elem);},

Original issue reported on code.google.com by akshar.dave on 18 Feb 2009 at 3:17

Attachments:

GoogleCodeExporter commented 8 years ago
I am having the same problem when I repopulate a div with the results of an ajax
query.  After a few calls, I get the elem.parentNode is null exception and all
ratings stop working.

The error is getting throw from the jquery siblings function.

Original comment by andrew.j...@gmail.com on 23 Feb 2009 at 3:19

GoogleCodeExporter commented 8 years ago
It appears that after reloading, the plugin's rating.groups property was not 
getting
reset, which was causing things to get confused.

I added the line:

        $.rating.groups = {};

to the beginning of the $.fn.rating function and things are now working 
properly again.

Original comment by andrew.j...@gmail.com on 23 Feb 2009 at 4:57

GoogleCodeExporter commented 8 years ago
I had this issue as well (like, 20 minutes ago) in dealing with AJAX-type 
scenarios.

Attached is a diff that cleanly resolves the issue.

Set 'overwrite' to 'true' to make this work.

andrew's solution technically works but you'll have problems later on when 
you're
mixing and matching ajax and non-ajax rendered ratings. :)

Example of using the patch:
$('input[type=radio]').validate({ overwrite: true })

Original comment by l33t...@gmail.com on 24 Feb 2009 at 1:05

Attachments:

GoogleCodeExporter commented 8 years ago
Sorry, I meant 'ratings()' in the above example -- I don't know what came over 
me
there. Must be working with too many jQuery plugins lately...

Original comment by l33t...@gmail.com on 24 Feb 2009 at 1:05

GoogleCodeExporter commented 8 years ago
I will give it a shot l33ts0n. Thanks

Original comment by akshar.dave on 24 Feb 2009 at 1:17

GoogleCodeExporter commented 8 years ago
Hi l33ts0n-

Can you please send me copy of patched version of jquery.ratings.js.

Thanks.

Original comment by akshar.dave on 1 Mar 2009 at 9:52

GoogleCodeExporter commented 8 years ago
Hi Askhar, I've got a modified copy of jquery.ratings.js unfortunately.

You can apply the patch using diff on Linux or Mac OS X; if you're using 
windows,
open the file in Notepad and look at the changes. The - is what was removed, 
the + is
what replaced it. There are only 2-3 modified lines, you should be able to 
modify
your copy of jquery.ratings.js easily by hand.

Original comment by l33t...@gmail.com on 1 Mar 2009 at 11:22

GoogleCodeExporter commented 8 years ago
I've been involved on other projects and haven't had a chance to attend to this 
issue 
until now. Thank you so much for your patch l33ts0n. I'm going to update the 
plugin 
(along with some new features and bug fixes) and release a new version soon.

From now on I'm going to announce new releases on this twitter account:
http://twitter.com/fyneworks

Original comment by diego.a...@gmail.com on 12 Mar 2009 at 9:17

GoogleCodeExporter commented 8 years ago

Original comment by diego.a...@gmail.com on 12 Mar 2009 at 9:18

GoogleCodeExporter commented 8 years ago
@ l33ts0n:

After reviewing your code, I see there are some problems in the logic of your 
patch:
if(!$.rating.groups[n] || (settings.overwrite && !processed[n])) 
$.rating.groups[n] = 
{count: 0};

The first part of the argument will ALWAYS be false because it's preceded by 
this line:
if(!$.rating.groups[n]) $.rating.groups[n] = {count: 0};

And the second part of the argument will ALWAYS be false for the first element 
of a group 
because the 'processed' variable is declared locally. This means each time a 
call is made 
to the plugin, the group will be reset.

I thought the root of the problem is that when you call the plugin for a second 
time (eg.: 
after an ajax call / dom update) it runs over its own previous installation. 
The quick way 
to fix this is to mark every processed element with a class, and ignore them in 
future 
calls (as they are 'ready').

However, I can't see how this would happen because the checkboxes used to 
gerenate the 
star rating widget are removed from DOM - therefore they can't be matched by 
jQuery and 
can't possibly be the subject of a second plugin call.

Then I realised, 'group names' are based on the element's name, which assumes 
you will 
only have 1 group of elements with the same name in 1 call. If you call the 
plugin for a 
second time and another group of elements (presumably within a separate form) 
has not yet 
been initialized but happens to have the same name as a group of elements 
already 
processed by the plugin, it assumes that the new elements belong to the same 
old group of 
elements. I hope that made sense....

SO, in conclusion, the real fix to the problem is to make element groups unique 
to each 
plugin call. That way, when you call the plugin again on a new group of 
elements with the 
same name of a previously initialized group of elements, the internal group 
data will not 
exist, the new group data will be created.

Last but not least, I can see how this may lead to a memory leak (storing 
information 
about old non-existing elements) so I will work on the plugin next week to 
start using 
jQuery data storage facilities...

The website and repository have been updated. You can download the new version 
(2.62) from 
the usual places...

Original comment by diego.a...@gmail.com on 12 Mar 2009 at 10:19

GoogleCodeExporter commented 8 years ago
Sweet, thanks.

Glad you caught what I was going for, even though it didn't work. :)

Original comment by l33t...@gmail.com on 12 Mar 2009 at 7:06

GoogleCodeExporter commented 8 years ago
Oh!

I should inform you that 2.62 seems to be a breaking change -- the input names 
submitted via the form are changed. For instance, 'review_score' becomes 
'1_review_score' which breaks existing code expecting 'review_score.'

Original comment by l33t...@gmail.com on 12 Mar 2009 at 8:00

GoogleCodeExporter commented 8 years ago
Wow! Stupid oversight on my part. I've fixed the problem and re-released the 
plugin, 
v2.63 (so others know to pick up the updated version).

Thanks!

Original comment by diego.a...@gmail.com on 13 Mar 2009 at 2:53