Erik
Erik

Reputation: 14770

How to store date field as moment.js objects in Backbone.Model?

I have Backbone.Model like the following:

{
   id: '1',
   startTime: 'Sun May 04 2014 13:00:00 GMT+0400 (MSK)',
   endTime: 'Sun May 04 2014 13:29:59 GMT+0400 (MSK)',
}

At now I store startTime and endTime as strings in my database but on client side I want to store them as moment.js objects for handy working with them.

How could I store them as momment.js in my Backbone.Model to be possible to get and set as moment.js objects but store on server side as pure strings?

Should I use some Backbone extension?

Upvotes: 0

Views: 1047

Answers (1)

user3600963
user3600963

Reputation: 36

What about this?

yourModel.set('startTime', moment("Dec 25, 1995"));

You can convert after fetch like this,

yourModel.parse = function(response) {
    response.startTime = moment(response.startTime);
    return response;    
};

Upvotes: 2

Related Questions