[ADD] JSONify Date objects to a UTC datetime in OpenERP server format

This way, the client can send genuine local Date objects without
caring too much, the server will get them in an expected format. This
can then be used by leaving Date objects in domains, and have these
date objects end up correctly in the server without creators of
domains having to manually serialize them to UTC from a local datetime
somehow.

bzr revid: xmo@openerp.com-20140122135426-zm80cj5wm9iebs76
This commit is contained in:
Xavier Morel 2014-01-22 14:54:26 +01:00
parent 0f2d7e4820
commit 36eedfab43
1 changed files with 22 additions and 2 deletions

View File

@ -823,13 +823,33 @@ var genericJsonRpc = function(fct_name, params, fct) {
});
};
/**
* Replacer function for JSON.stringify, serializes Date objects to UTC
* datetime in the OpenERP Server format.
*
* However, if a serialized value has a toJSON method that method is called
* *before* the replacer is invoked. Date#toJSON exists, and thus the value
* passed to the replacer is a string, the original Date has to be fetched
* on the parent object (which is provided as the replacer's context).
*
* @param {String} k
* @param {Object} v
* @returns {Object}
*/
function date_to_utc(k, v) {
var value = this[k];
if (!(value instanceof Date)) { return v; }
return openerp.datetime_to_str(value);
}
openerp.jsonRpc = function(url, fct_name, params, settings) {
return genericJsonRpc(fct_name, params, function(data) {
return $.ajax(url, _.extend({}, settings, {
url: url,
dataType: 'json',
type: 'POST',
data: JSON.stringify(data),
data: JSON.stringify(data, date_to_utc),
contentType: 'application/json'
}));
});
@ -838,7 +858,7 @@ openerp.jsonRpc = function(url, fct_name, params, settings) {
openerp.jsonpRpc = function(url, fct_name, params, settings) {
settings = settings || {};
return genericJsonRpc(fct_name, params, function(data) {
var payload_str = JSON.stringify(data);
var payload_str = JSON.stringify(data, date_to_utc);
var payload_url = $.param({r:payload_str});
var force2step = settings.force2step || false;
delete settings.force2step;