From 2781fb1643835d8c6dc2510194dbc22e4b60632d Mon Sep 17 00:00:00 2001 From: Newton Muchael Date: Wed, 5 Dec 2018 17:28:57 -0200 Subject: [PATCH] =?UTF-8?q?Vers=C3=A3o=202.2.0=20-=20Converter=20para=20Mo?= =?UTF-8?q?ment.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../resources/templates/services-wrapper.ts | 36 ++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 9aaa2f8..45c4f43 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ br.com.eits.codegen ts-codegen-maven-plugin maven-plugin - 2.1.3 + 2.2.0 diff --git a/src/main/resources/templates/services-wrapper.ts b/src/main/resources/templates/services-wrapper.ts index 4291e7d..dc8a7c7 100644 --- a/src/main/resources/templates/services-wrapper.ts +++ b/src/main/resources/templates/services-wrapper.ts @@ -5,7 +5,8 @@ import { Observable, Observer } from 'rxjs'; * path é o caminho SEM BARRA AO FINAL para o broker. Por padrão é simplesmente 'broker' */ export interface BrokerConfiguration { - path: string + path: string, + useMoment?: boolean } export let BROKER_CONFIGURATION = new InjectionToken('broker.configuration'); @@ -40,19 +41,46 @@ export function dwrWrapper(configuration: BrokerConfiguration, serviceName: stri function loadDwrIfNeeded(configuration: BrokerConfiguration): Promise { return new Promise(resolve => { if ((window as any).dwr) { - resolve(); + if ( configuration.useMoment ) { + shimDwrThenResolve( resolve ); + } else { + resolve(); + } } else { const path = `${configuration.path}/engine.js`; const tag: HTMLScriptElement = document.createElement('script'); tag.src = path; tag.type = 'text/javascript'; - tag.onload = () => resolve(); - tag.onerror = () => resolve(); + if ( configuration.useMoment ) { + tag.onload = () => shimDwrThenResolve( resolve ); + tag.onerror = () => shimDwrThenResolve( resolve ); + } else { + tag.onload = () => resolve(); + tag.onerror = () => resolve(); + } document.body.appendChild(tag); } }); } +/** + * Intercepta as chamadas de convert do dwr para converter o tipo Moment para Date caso ele esteja sendo utilizado + * @param resolve + */ +function shimDwrThenResolve(resolve: Function) { + (function(dwr) { + const original = dwr.engine.serialize.convert; + dwr.engine.serialize.convert = function(batch, directrefmap, otherrefmap, data, name, depth) { + if (data != null && typeof data == 'object' && data._isAMomentObject) { + original(batch, directrefmap, otherrefmap, data.toDate(), name, depth); + } else { + original(batch, directrefmap, otherrefmap, data, name, depth); + } + }; + })(window['dwr']); + + resolve(); +} function loadServiceIfNeeded(configuration: BrokerConfiguration, name: string): Promise { return new Promise(resolve => {