Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to modify the date format when ember-data does serialization?

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

like image 423
Toran Billups Avatar asked Mar 29 '13 01:03

Toran Billups


People also ask

What is serializer in Ember?

In Ember Data, serializers format the data sent to and received from the backend store. By default, Ember Data serializes data using the JSON:API format. If your backend uses a different format, Ember Data allows you to customize the serializer or use a different serializer entirely.

What is the default serializer shipped with Ember?

Ember Data ships with 3 Serializers. The JSONAPISerializer is the default serializer and works with JSON API backends. The JSONSerializer is a simple serializer for working with single json object or arrays of records.

How do I use Ember data?

It is the main interface you use to interact with Ember Data. In this case, call the findAll function on the store and provide it with the name of your newly created rental model class. import Route from '@ember/routing/route'; export default Route. extend({ model() { return this.

What is a serializer in Ember Data?

In Ember Data, serializers format the data sent to and received from the backend store. By default, Ember Data serializes data using the JSON:API format. If your backend uses a different format, Ember Data allows you to customize the serializer or use a different serializer entirely.

What is a model in Ember Data?

In Ember Data, models are objects that represent the underlying data that your application presents to the user. Note that Ember Data models are a different concept than the model method on Routes, although they share the same name.

What is normalized JSON in Ember Data?

The normalized JSON format that Ember Data expects is a JSON:API document with a couple of additional restrictions. First, it is important to make sure that the type name of a record in the normalized JSON object exactly matches the filename of the model defined for this record type.

Does Ember Data support plural or singular inflection rules in JSON?

The JSON:API spec itself is agnostic about inflection rules, however, Ember Data's own JSONAPISerializer assumes types are plural and it will automatically singularize the types.


Video Answer


4 Answers

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

like image 164
Jakub Arnold Avatar answered Oct 11 '22 04:10

Jakub Arnold


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.

like image 24
Sapan Diwakar Avatar answered Oct 11 '22 05:10

Sapan Diwakar


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.

like image 8
user160917 Avatar answered Oct 11 '22 05:10

user160917


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;
      }
    }
  });
like image 2
Graeme Stuart Avatar answered Oct 11 '22 05:10

Graeme Stuart