Needs review
Project:
JSON server
Version:
6.x-2.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
15 Sep 2011 at 20:47 UTC
Updated:
15 Sep 2011 at 20:47 UTC
I've added/modified the json_server module to support localized calls to the services module, and thought it would be good to share it here.
First, in json_server.js, we replace Drupal.settings.baseurl with Drupal.settings.basePath, which is defined by Drupal core. Secondly, we make use of an updated service url which we'll define in our module.
Drupal.service = function(service_method, args, success) {
args = $.extend({method: service_method}, args);
args_done = {};
for(i in args) {
args_done[i] = Drupal.toJson(args[i]);
}
$.post(Drupal.settings.basePath + Drupal.settings.json_server.url, args_done, function(unparsed_data) {
parsed_data = Drupal.parseJson(unparsed_data);
success(!parsed_data['#error'], parsed_data['#data']);
});
};
Then, in json_server.module we check for the language global, and add the appropriate prefix if it exists to the service request URL, defining the Drupal.settings.json_server.url parameter for our js.
function json_server_init() {
global $language;
$lang_prefix = isset($language->prefix)?$language->prefix."/":'';
$server_url = "?q=".$lang_prefix.'services/json';
$path = drupal_get_path("module", "json_server");
drupal_add_js($path ."/json_server.js");
drupal_add_js(array("json_server" => array('url'=>$server_url)), 'setting');
}