Posted by lklimek on January 27, 2013 at 1:32am
This module exposes all variables defined with Variable API to JavaScript API.
Usage
- Checkout and enable module
- In hook_variable_info() define your variables to be exportable to JavaScript
- Access your variables from Javascript with code like:
Drupal.settings.variable_js.variableName(note that changes will not persist).
Example handler
<?php
function mymodule_variable_info() {
$variables = array();
$variables['mymodule_info'] = array(
'title' => t('Info text'),
'description' => t('Sample variable with information'),
'type' => 'string',
'token' => TRUE,
'multidomain' => TRUE,
'js' => TRUE,
);
return $variables;
}
?>Example JavaScript usage
function($) {
Drupal.behaviors.mymodule_test = {
attach : function(context) {
alert(Drupal.settings.variable_js.mymodule_info);
}
}
})(jQuery);Note: examples were not tested on animals ;-).
Dependencies
Requires Variable.
To do
Some ideas for improvement:
- Allow exporting to JS of variables from GUI (add a checkbox in variable GUI that enables any variable to be exportable to JS)
- Allow saving of variable modifications made in JavaScript (some AJAX magic will be required)
Currently I don't have time / motivation / need for these, but if you find any of them interesting - let me know / create a feature request issue.