20

In my ember-data adapter I use this line to serialize my model

var data  = record.serialize();

But I've noticed my models with a date type ...

App.Foo = DS.Model.extend({
    start: DS.attr('date')
});

... will post the date like this to my REST api

Sat, 02 Mar 2013 22:15:00 GMT

But I need something more api friendly such as yyyy-mm-dd or mm/dd/yyyy

Does ember-data offer a hook to change how dates are sent over the wire?

I would assume not as this is the actual return line from the serialize method in ember-data rev 11

return dayOfWeek + ", " + dayOfMonth + " " + month + " " + utcYear + " " + pad(utcHours) + ":" + pad(utcMinutes) + ":" + pad(utcSeconds) + " GMT";

Update

I also opened an issue on ember-data to see why this format was chosen to begin with

https://github.com/emberjs/data/issues/845

Toran Billups
  • 27,111
  • 40
  • 155
  • 268

4 Answers4

14

You could register a custom serializer transform

DS.RESTAdapter.registerTransform("isodate", {
  deserialize: function(serialized) {
    return serialized;
  },

  serialize: function(deserialized) {
    return deserialized;
  }
});

and then just use it as start: DS.attr("isodate"), with proper definitions of serialize/deserialize of course :)

Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
12

You can register a new transform as Jakub suggested in this answer. With Ember Data 1.0.beta.1, you have to create a new transform on the app like so:

App.IsodateTransform = DS.Transform.extend({
  deserialize: function (serialized) {
    if (serialized) {
      return moment(serialized).toDate();
    }
    return serialized;
  },

  serialize: function (deserialized) {
    if (deserialized) {
      return moment(deserialized).toISOString();
    }
    return deserialized;
  }
});

You can to change the serialize and deserialize definitions if not using moment.js.

Community
  • 1
  • 1
Sapan Diwakar
  • 10,480
  • 6
  • 33
  • 43
8

Ember-Rails Solution:

I'm using ember-data 0.13 / ember.js 1.0.0.rc5 in an ember-rails app. I found I had to do the following to match up javascript dates with rails dates.

# Transforms Date to avoid miss-match with rails date
DS.JSONTransforms.isodate =
  deserialize: (serialized) ->
    if serialized
      date = new Date(serialized)
      offset = date.getTimezoneOffset()
      new Date(date.getTime()+offset*60000)
    else 
      null
    
  serialize: (date) ->
    if date then moment(date).format("YYYY-MM-DD") else null

*I'm using moment.js to do the serialization, but it probably wouldn't be to hard to do on your own.

Community
  • 1
  • 1
user160917
  • 9,211
  • 4
  • 53
  • 63
  • 2
    Was going to edit, but decided a comment might be better. I needed to use `App.IsodateTransform = DS.Transform.extend` instead of `DS.JSONTransforms.isodate` for ember-data 1.0.0.beta.6. I'm also using the `DS.ActiveModelAdapter` adapter. – Peter Brown Feb 17 '14 at 17:13
  • @Beerlington It would be cooler to edit the original reply as it doesn't work right now. – Jakub Aug 29 '14 at 14:03
2

A version that doesn't rely on moment.js

taken from https://github.com/toranb/ember-data-django-rest-adapter/issues/26

  App.IsodateTransform = DS.Transform.extend({
    deserialize: function(serialized) {

      var type = typeof serialized;

      if (type === "string") {
        return new Date(Ember.Date.parse(serialized));
      } else if (type === "number") {
        return new Date(serialized);
      } else if (serialized === null || serialized === undefined) {
        // if the value is not present in the data,
        // return undefined, not null.
        return serialized;
      } else {
        return null;
      }
    },

    serialize: function(date) {
      if (date instanceof Date) {
        return date.toJSON();
      } else {
        return null;
      }
    }
  });
Graeme Stuart
  • 5,837
  • 2
  • 26
  • 46