javascript 如何在 Backbone 中处理日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9130744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle dates in Backbone?
提问by Bart Jacobs
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 initializemethod. So far so good.
我将日期以 DATETIME 格式存储在 MySQL 数据库中。当从数据库中获取模型时,日期(采用 DATETIME 格式)在模型的initialize方法中转换为日期对象。到现在为止还挺好。
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 savemethod can I safely convert the dates to the DATETIME format?
将模型保存到服务器时,需要将日期对象转换回 DATETIME 格式,以便服务器端代码正确解释日期。我已经使用了各种技巧来做到这一点,但我想知道在模型的保存方法的哪个阶段我可以安全地将日期转换为 DATETIME 格式?
One approach is to do the following:
一种方法是执行以下操作:
this.model.save({
date : date.toDateTime()
}, options);
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).
但是,这会导致触发更改事件,因为 save 方法之前的属性哈希与 save 事件之后的哈希不同(这会触发 Backbone 的 set 方法)。
采纳答案by Tomasz Nurkiewicz
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
.
我建议在模型和界面中使用UNIX 时间(从 1970 年开始的秒数/毫秒数),并仅在View
.
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:
因此,服务器以数字形式发送和接收日期1328281766454
,这就是您将它们存储在Backbone.Model
. 当它必须被渲染时,View
你可以简单地调用:
new Date(this.model.get('someTime')); //Fri Feb 03 2012 16:09:26 GMT+0100 (CET)
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.
在服务器端也可以这样做。相信我,这是在没有所有这些时区问题的情况下传输日期的最简单、最便携的方式。
回答by Rob Hruska
I'd do it one of two places:
我会做两个地方之一:
On the server:
This probably makes the most sense, since your server-side implementation is really the one that needs the DATETIME representation; your client code shouldn't have to care at all.
In
toJSON()
on your model:If you mustdo it on the client, override
Backbone.Model
'stoJSON()
for your model and update it there. Example:toJSON: function () { var json = Backbone.Model.prototype.toJSON.call(this); json.date = convertDate(this.get('date')); return json; }
If you do this, you'll need to convert the date back, either in your model's
initialize()
orparse()
function.
在服务器上:
这可能是最有意义的,因为您的服务器端实现确实需要 DATETIME 表示;您的客户端代码根本不需要关心。
在
toJSON()
您的模型中:如果您必须在客户端上执行此操作,请为您的模型覆盖
Backbone.Model
'stoJSON()
并在那里更新它。例子:toJSON: function () { var json = Backbone.Model.prototype.toJSON.call(this); json.date = convertDate(this.get('date')); return json; }
如果这样做,则需要在模型
initialize()
或parse()
函数中将日期转换回来。
回答by Michael Kennedy
May not be the answer your looking for - but have seen folks use moment.js to format time in backbone -
可能不是您要寻找的答案 - 但已经看到人们使用 moment.js 在主干中格式化时间 -