From c5591bd039376f966137960eb2186982548c1ba8 Mon Sep 17 00:00:00 2001 From: Jakub Szwacz Date: Mon, 30 Jun 2014 21:21:53 +0200 Subject: [PATCH] Fix for strange Date behavior in Node Webkit --- lib/model.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/model.js b/lib/model.js index f86af72..5bbf1f7 100644 --- a/lib/model.js +++ b/lib/model.js @@ -5,9 +5,7 @@ * Querying, update */ -var dateToJSON = function () { return { $$date: this.getTime() }; } - , originalDateToJSON = Date.prototype.toJSON - , util = require('util') +var util = require('util') , _ = require('underscore') , modifierFunctions = {} , lastStepModifierFunctions = {} @@ -66,22 +64,21 @@ function checkObject (obj) { */ function serialize (obj) { var res; - - // Keep track of the fact that this is a Date object - Date.prototype.toJSON = dateToJSON; - + res = JSON.stringify(obj, function (k, v) { checkKey(k, v); + + if (v === undefined) { return undefined; } + if (v === null) { return null; } - if (typeof v === undefined) { return null; } - if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean' || v === null) { return v; } + // Hackish way of checking if object is Date (this way it works between execution contexts in node-webkit). + // We can't use value directly because for dates it is already string in this function (date.toJSON was already called), + // fortunately "this" is bound here to object which is owner of the key. + if (typeof this[k].getTime === 'function') { return { $$date: this[k].getTime() }; } return v; }); - // Return Date to its original state - Date.prototype.toJSON = originalDateToJSON; - return res; }