In /views_datasource/views/theme/views-views-json-style-simple.tpl.php the following code:

  if (isset($_GET[$jsonp_prefix]) && $jsonp_prefix) {
    $json = check_plain($_GET[$jsonp_prefix]) . '(' . $json . ')';
  }

guarantees that the prefix saved in the views settings here is never used:
JSONP Prefix

It's easy enough to save a copy of views-views-json-style-simple.tpl.php in my theme to override the template, but shouldn't the code itself look like this instead?

  if ($jsonp_prefix) {
    $json = $jsonp_prefix . '(' . $json . ')';
  }

Or maybe I'm just missing the point here? Under what conditions would you ever want a $_GET value here instead of the value set in the view?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

codevoice created an issue. See original summary.

renat’s picture

Version: 7.x-1.0-alpha2 » 7.x-1.x-dev
Priority: Normal » Major
Status: Active » Needs review
FileSize
2.69 KB

Code itself is more or less OK, but user interface is extremely misleading - to the point where it's not possible to use JSONP format unless you're already comfortable with it, and can read module's code.

"JSONP Prefix" input is actually to set URL parameter NAME, and with that name we can get prefix VALUE from the URL. For example, we should query Views Datasource-generated page from our client JS code like this:
https://yoursite.com/path/to/api?prefixName=myJsFunction
So you should put "prefixName" to "JSONP Prefix" input, and we'll get
myJsFunction({"nodes":[{"node":{"title":"Some text"}}]})
in response. So the module behaves in line with JSONP standard, client app should be able to specify JSONP prefix - as it's a name of the function, that will be called back on the client upon server response.

Here is the patch, that fixes "JSONP Prefix" input name and description, and adds JSONP-related instructions to README file.

NicholasS’s picture

Using isset() seems to be the number one bug I always see in Drupal modules.

I don't see why people don't always just use !empty() instead.

I maybe would also phrase it as....

'#description' => t('If used this will identify the callback URL parameter. This parameters value will be used to prefix the JSON into a JSONP format.'),