如何处理骨干的日期? [英] How to handle dates in Backbone?

查看:112
本文介绍了如何处理骨干的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在存储在MySQL数据库中的DATETIME格式的日期。当一个模型是从数据库中提取,日期(在DATETIME格式)转换为Date对象模型的的初始化的方法。到目前为止好。

I store dates in the DATETIME format in a MySQL database. When a model is fetched from the database, dates (in the DATETIME format) are converted to date objects in the model's initialize method. So far so good.

当模型被保存到服务器中,日期对象需要才能被转换回DATETIME格式在服务器端code国米preT正确的日期。我已经使用了各种两轮牛车要做到这一点,但我不知道在什么阶段可以将模型的的保存的方法,我可以放心地将日期转换为DATETIME格式?

When the model is saved to the server, the date objects need to be converted back to the DATETIME format in order for the server side code to interpret the dates correctly. I have used all sorts of hackery to do this, but I wonder at what stage can of the model's save method can I safely convert the dates to the DATETIME format?

一种方法是做到以下几点:

One approach is to do the following:

this.model.save({
    date : date.toDateTime()
}, options);

然而,这会导致由于属性散列之前,保存方法是不一样的保存事件之后哈希被触发更改事件(这会触发骨干set方法)。

However, this causes a change event to be fired since the attributes hash before the save method is not the same as the hash after the save event (and this triggers Backbone's set method).

推荐答案

我会建议使用 UNIX时间(数从1970年秒/分毫秒),无论是在模型和接口,只在查看

I would advice using UNIX time (number of seconds/milliseconds from 1970) both in model and in the interface and converting to readable date only in View.

所以,服务器都会发送和接收日期为数字像 1328281766454 ,这是你他们如何存储在 Backbone.Model 。当它在被渲染查看您可以简单地调用:

So the server both sends and receives dates as numbers like 1328281766454 and this is how you store them in Backbone.Model. When it has to be rendered in View you can simply call:

new Date(this.model.get('someTime'));  //Fri Feb 03 2012 16:09:26 GMT+0100 (CET)

同样可以在服务器端进行。相信我,这是transfrering日期没有这些时区问题的最简单,最简便的方式。

The same can be done on the server side. Believe me, this is the simplest and most portable way of transfrering dates without all these time-zone issues.

这篇关于如何处理骨干的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆