Should the basic RESTAdapter send dates formatted in the text format? #845

Closed
toranb opened this Issue Mar 29, 2013 · 3 comments

Projects

None yet

3 participants

@toranb
Contributor
toranb commented Mar 29, 2013

Currently when I invoke createRecord on the RESTAdapter each date field on my model is serialized like so

    createRecord: function(store, type, record) {                              
      var root = this.rootForType(type);

      var data = {};
      data[root] = this.serialize(record, { includeId: true });

When I watch chrome dev tools I've found the date looks something like this as it goes over the wire

Sat, 02 Mar 2013 22:15:00 GMT

As I'm not using a rails backend I have to ask -is this the best format to send a date value over the wire? My django backend would prefer something like yyyy-m-dd or m/dd/yyyy and I'm curious how a patch for something like this (in ember-data core) would occur as the base serializer assumes the format mentioned above

    serialize: function(date) { 
      if (date instanceof Date) {
        var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
        var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", >

        var pad = function(num) {
          return num < 10 ? "0"+num : ""+num;
        };

        var utcYear = date.getUTCFullYear(),
            utcMonth = date.getUTCMonth(),
            utcDayOfMonth = date.getUTCDate(),
            utcDay = date.getUTCDay(),
            utcHours = date.getUTCHours(),
            utcMinutes = date.getUTCMinutes(),                                 
            utcSeconds = date.getUTCSeconds();

        var dayOfWeek = days[utcDay];
        var dayOfMonth = pad(utcDayOfMonth);
        var month = months[utcMonth];

    return dayOfWeek + ", " + dayOfMonth + " " + month + " " + utcYear + " " + pad(utcHours) + ":" + pad(utcMinutes) + ":" + pad(utcSeconds) + " GMT";
      } else if (date === undefined) {
        return undefined;
      } else {
        return null;
      }
    }
@toranb
Contributor
toranb commented Mar 29, 2013

What I would prefer looks something like this

return utcYear + "-" + pad(utcMonth) + "-" + pad(utcDay) + "T" + pad(utcHours) + ":" + pad(utcMinutes) + ":" + pad(utcSeconds) + "Z";
@cyril-sf
Contributor
cyril-sf commented Apr 3, 2013

I would look to a custom DS.attr (I don't know if this is still supported) or registerTransform on the adapter

@igorT
Member
igorT commented Apr 5, 2013

As @Cyril-sf said you can register a transform for your custom date property. We do that as well.

@igorT igorT closed this Apr 5, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment