Hey there MikeyTown, following up on your comment over here: http://echodittolabs.org/blog/2011/04/setting-js-callbacks-drupal-json-a... regarding making a more generalized solution for the jQuery.extend workaround to the encode_json command's inability to output non-quoted string values that would be necessary to set JSON key values to callback functions (instead of their string equivalent).

On first thought, I envision a command something like drupal_add_js_settings_objects() that would take similar args to drupal_add_js but output either (a) a jQuery.extend command or (b) use a different lib to encode the json, if one can be found that supports non-string value output. A quick search turned up this page reviewing options in 2006, http://gggeek.altervista.org/sw/article_20061113.html, but I don't know about the specifics of each or the current state.

What are others thinking?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mikeytown2’s picture

drupal_json_encode doesn't do the trick.

This might do it: http://php.net/json-encode#101497
drupal.stackexchange issue: http://drupal.stackexchange.com/questions/1176/drupal-to-js-and-anonymou...
Not related to this but something I could fix (for settings) while I'm here: #1086098: D6 JSON is total garbage - Create RFC 4627 compliant HTML safe JSON

mikeytown2’s picture

FileSize
3.98 KB

This patch incorporates #1086098: D6 JSON is total garbage - Create RFC 4627 compliant HTML safe JSON still doesn't address calling a js function though. It's been committed.

mikeytown2’s picture

I think the only way to do this is via string replacements. I was thinking we could have something like DRUPAL_JS_RAW be the token that it looks for in the string. If it sees this, it will then take the strings contents and convert it.

array('DRUPAL_JS_RAW:alert:DRUPAL_JS_RAW') would then be converted to alert instead of 'alert' like it is right now.

mikeytown2’s picture

Status: Active » Needs review
FileSize
760 bytes

Here is your example

  $jcarousel_settings= array (
    'jcarousel' => array (
      'carousels' => array (
        'jcarousel-view--frontpage-slider--block-1' => array (
          'initCallback' => 'DRUPAL_JS_RAW:jcarousel_setPager:DRUPAL_JS_RAW',
          'itemVisibleInCallback' => array(
            'onAfterAnimation' => 'DRUPAL_JS_RAW:jcarousel_setActive:DRUPAL_JS_RAW',
          ),
        ),
      ),
    ),
  );
drupal_add_js($jcarousel_settings,'setting');

And here is the output:

"jcarousel":{
  "carousels":{
    "jcarousel-view--frontpage-slider--block-1":{
      "initCallback":jcarousel_setPager,
      "itemVisibleInCallback":{
        "onAfterAnimation":jcarousel_setActive
      }
    }
  }
}

Patch attached. Thoughts?

mikeytown2’s picture

Status: Needs review » Fixed

committed this :)

mikeytown2’s picture

FileSize
2.1 KB

committed this patch as well

Status: Fixed » Closed (fixed)

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

JeremyFrench’s picture

This looks great. Is it something worth raising as a D8 issue?

mikeytown2’s picture

@JeremyFrench
I would say say yes.