Since I have updated the rate module to 1.4 Ajax does not work any more for voting. That means, that the whole page is reloaded by clicking the rate button. The cause of that problem is, that the rate.js is included before the jquery.js. This leads in Javascript error:
ReferenceError: jQuery is not defined @ ***/sites/all/modules/rate/rate.js?mcndu4:60

Comments

gunwald’s picture

I changed line 292 in rate.module from:

drupal_add_js(drupal_get_path('module', 'rate') . '/rate.js', array('scope' => 'footer'));

to

drupal_add_js(drupal_get_path('module', 'rate') . '/rate.js', array('scope' => 'footer', 'group' => JS_DEFAULT,  'weight' => 200));

Just for testing, but did not help. The I commented the line out and added the line

scripts[] = rate.js

to rate.info.
Now it works again. I know that this is not a good solution, because now the JS is loaded every time on every page. But I am a bit helpless at that point not having any idea what could be the cause of that strange issue.

gunwald’s picture

With the above described workaround Ajax does not work if content was loaded via Ajax it self.

XmhO’s picture

I guess my issue is related to yours. Since fresh install of 1.4, I've got "jQuery is not defined" errors for both rate.js and fivestar.js (I'm using fivestar widget).
I'm using Fivestar via "rate_embed" function (I'm embedding fivestar widget in a display suite field).

The error is caused by too early loading of rate.js and fivestar.js : if you look at JS loading order, you can see that these JS are loaded before jquery.js.

gunwald’s picture

I actually fond out that skipping the 'scope' brings drupal_add_js() back to work. I don't have an idea why. So for rate widget I changed line 292 in rate.module in:

drupal_add_js(drupal_get_path('module', 'rate') . '/rate.js', array('group' => JS_DEFAULT));

It looks to me like a Drupal bug. The problem is, that AJAX loaded widgets keep refuse working. I get the error:

TypeError: Drupal.settings.rate.basePath.match is not a function
Source: .../rate/rate.js?md0yni
line: 31 

The cause of that error seems to be that the code form line 286-291 is not being executed or perhaps not at the right moment?

  drupal_add_js(array(
    'rate' => array(
      'basePath' => url('rate/vote/js'),
      'destination' => $dest['destination'],
    )
  ), array('type' => 'setting'));

Does anybody know where o what could cause that strange behavior?

XmhO’s picture

I fixed my problem by specifying another scope for rate JS files :

<?php
/**
 * Implements hook_js_alter().
 * Rate JS Order fix...
 */
function MYMODULE_js_alter(&$js) {
  $rateModulePath = drupal_get_path('module', 'rate');
  foreach($js as $file => $info) {
    if (strpos($file, $rateModulePath) === 0) {
      $js[$file]['scope'] = 'header';
    }
  }
}
?>

It's not a good solution but It fixes my problem. Can you try this snippet gunwald in order to see if our error messages are based on same issue?

gunwald’s picture

Thanks XmhO, it works. But apart from the fact, that your solution is much more elegant then mine, both do the same: Deleting or changing the scope. Bu why have we to do so? And why does it not work with AJAX loaded content? XmhO, do you have a widget witch is loaded with AJAX? Just to tray whether or not you experience the same problem? Thank you so far!

mauritsl’s picture

Status: Active » Fixed

The Javascript was moved to the footer for performance reasons, see #1717484: Move JavaScript to footer. The script should be loaded after the other scripts (and content) are loaded, but it seems to have the opposite effect in some cases.

I have reverted this. Too much problems to keep it.

Original commit:
http://drupalcode.org/project/rate.git/commitdiff/739768c

gunwald’s picture

Thank you mauritsl. Reverting this two lines solves the first issue: The rate.js is loaded with the right weight again. But I still get an JavaScript error when I click on a rate widget which was loaded via AJAX because of the fact that Drupal.setting.rate.basePath seems not to be set.
Maybe that is a different issue? Could that have something to do with the function drupal_get_destination()? Maybe it does not return a result when the script is called via AJAX?
Thank you very much for your help!

gunwald’s picture

I hunted it down. The problem is, that for some reason the JavaScript variable set in line 286 in rate.module "Drupal.settings.rate.basePath" and "Drupal.settings.rate.destination":

  drupal_add_js(array(
    'rate' => array(
      'basePath' => url('rate/vote/js'),
      'destination' => $dest['destination'],
    )
  ), array('type' => 'setting'));

becomes an Array instead of a string when the content containing the widget is loaded via Ajax. The cause of that problem may be an bug in drupal_add_js() core function because of the fact that documentation says if a setting value is set more than once it will be overwritten. How ever, in this case it is not, but becomes an array which looks like:

0 => "rate/vote/js",
1 => "rate/vote/js",
2 => "rate/vote/js",

I added, just for testing, these to lines to line before line 31 in rate.js:

Drupal.settings.rate.basePath = Drupal.settings.rate.basePath instanceof Array ? Drupal.settings.rate.basePath[0] : Drupal.settings.rate.basePath;
Drupal.settings.rate.destination = Drupal.settings.rate.destination instanceof Array ? Drupal.settings.rate.destination[0] : Drupal.settings.rate.destination;

Now it works again. I know it is a dirty workaround but I could not find the real cause of the problem. Would be glad to know whether or not someone can reproduce the described problem.

XmhO’s picture

I don't have anything loaded in Ajax, but my widget is displayed with "rate_embed" built-in function, inside a Display Suite field.

I guess It is not Ajax related.

gunwald’s picture

But the problem occurs only with content loaded via AJAX, I can't imagine the real cause but I did not had that issue with the former versions of Rate Widget. One could set a View's load more behavior to AJAX for testing.

XmhO’s picture

As mentioned above, the line has been reverted. It should work now, with last revert. Isn't it?

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Aurochs’s picture

didnt help me

rooby’s picture

For anyone coming across this issue (because there is still a similar issue to what some of you describe in the latest version), see #2102571: drupal_add_js() call turns basePath string into array