admin管理员组

文章数量:1191943

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.

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);

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).

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.

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);

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).

Share Improve this question asked Feb 3, 2012 at 15:04 Bart JacobsBart Jacobs 9,0827 gold badges49 silver badges89 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 9

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.

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)

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.

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 must do it on the client, override Backbone.Model's toJSON() 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() or parse() function.

May not be the answer your looking for - but have seen folks use moment.js to format time in backbone -

http://momentjs.com/

本文标签: javascriptHow to handle dates in BackboneStack Overflow