简体   繁体   English

jQuery datepicker + Rails 3.2.8 + AngularJS:显示和持久化日期问题

[英]jQuery datepicker + Rails 3.2.8 + AngularJS: show and persist date issue

I'm having an hard time making the triad rails+angularjs+jQuery datepicker work together. 我很难让三合一rails + angularjs + jQuery datepicker一起工作。

The first issue is with showing the date as retrieved from the backend. 第一个问题是显示从后端检索的日期。 When the UI requests data from the backend, at some point, it receives the following stuff in JSON notation. 当UI从后端请求数据时,它会在某个时刻以JSON表示法接收以下内容。

{"id":1,"ragione_sociale":"FrigoCaserta srl","indirizzo":"Strada provinciale Gricignano d'Aversa","cap":"81030","citta":"Gricignano d'Aversa","provincia":"CE","partita_iva":"1234","codice_fiscale":null,"tipo_contratto":"Orario","costo":"0.0","inizio":"2012-05-01","fine":"2013-09-22","$$hashKey":"005"}

The relevant fields are "inizio" and "fine" (respectively start and end dates). 相关字段是“inizio”和“fine”(分别是开始和结束日期)。

Rails timezone is set to Europe/Rome. Rails时区设置为欧洲/罗马。

Datepicker's options are set to: {dateFormat: 'dd/mm/yy'} Datepicker的选项设置为:{dateFormat:'dd / mm / yy'}

Thus I expected the input field to show the "inizio" date as: 01/05/2012 but, instead, I get back: 24/03/2018. 因此,我希望输入字段显示“inizio”日期为:01/05/2012但相反,我回来了:24/03/2018。

Changing the dateFormat option to 'yy-mm-dd' shows the correct date but with unwanted separator and format (2012-05-01) ... 将dateFormat选项更改为'yy-mm-dd'会显示正确的日期,但会显示不需要的分隔符和格式(2012-05-01)...

What happens behind the scenes, though, is that the displayed object gets somewhat manipulated and becomes like this: 然而,幕后发生的事情是,显示的对象受到某种程度的操纵,变成这样:

{"id":1,"ragione_sociale":"FrigoCaserta srl","indirizzo":"Strada provinciale Gricignano d'Aversa","cap":"81030","citta":"Gricignano d'Aversa","provincia":"CE","partita_iva":"1234","codice_fiscale":null,"tipo_contratto":"Orario","costo":"0.0","inizio":"2012-04-30T22:00:00.000Z", ...

Note the "inizio" field is now transformed from "2012-05-01" to "2012-04-30T22:00:00.000Z" 请注意,“inizio”字段现在已从“2012-05-01”转换为“2012-04-30T22:00:00.000Z”

Now, when I submit the form to the backend everything goes belly up, since rails receives the new value for "inizio" as formatted by the datepicker. 现在,当我将表单提交到后端时,所有内容都会变得很糟糕,因为rails会收到由datepicker格式化的“inizio”的新值。 The database field being just a Date makes the information about time disappear, and what's left is just the (wrong) date persisted into the model. 数据库字段只是一个日期,使得有关时间的信息消失,剩下的只是持久存入模型的(错误)日期。

I'm aware that datepicker has localization options, but I can't find anything timezone related I could change to fix this unwanted behaviour. 我知道datepicker有本地化选项,但是我找不到任何与时区相关的东西我可以改变以修复这种不需要的行为。

I thought I could use attributes on the backend's model to parse the information coming from UI, but I don't like this approach. 我以为我可以使用后端模型上的属性来解析来自UI的信息,但我不喜欢这种方法。

What's the best practice to follow when dealing with all this? 在处理所有这些问题时,最好的做法是什么?

I've gone through StackOverflow similar questions without luck. 我没有运气就遇到了StackOverflow类似的问题。

Thanks in advance for your help. 在此先感谢您的帮助。

Once this pull request makes its way into core, it should be easier as resource will support a decoder where this string to date translation can be done. 一旦这个拉取请求进入核心,它应该更容易,因为资源将支持解码器,其中可以完成此字符串到目前为止的转换。 https://github.com/angular/angular.js/pull/1514 https://github.com/angular/angular.js/pull/1514

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM