Registering transforms in Ember-Data Beta 2


#1

The registerTransform method has been removed, and the guides haven’t been updated yet. Looking at the source, it seems they are registered via initializers? How would I make my own to register my own transforms?


#2

Transforms are looked up in the container. Try this:

App.register('transform:my_transform', function(){...});

#3

I’m trying to find the answer to this as well. (Here’s a link to the initializers @pzuraq mentioned btw.)

@thomaslang’s solution is what I’m currently using, but it seems like there should be an easier way to do this. If nothing exists right now, what about something as simple as:

Ember.Application.reopen({
  registerTransform: function(type, transform) {
    // TODO: Probably needs some error checking here
    this.register('transform:' + type, transform);
  }
});

This way the old registerTransform syntax would be preserved. Users would only have to move their registrations from their adapter to their application:

// App.RESTAdapter.registerTransform('custom_type', {
//   serialize: function(value) { ... },
//   deserialize: function(value) { ... }
// });
//
// ...becomes...

App.registerTransform('custom_type', {
  serialize: function(value) { ... },
  deserialize: function(value) { ... }
});

Extending DS.Transform doesn’t seem to be mandatory, but might be advisable. This too would be an insignificant edit to existing code.


#4

I’ve since found this gem: https://github.com/emberjs/data/blob/master/TRANSITION.md#json-transforms

I’ve changed my app in accordance with this. However, I can’t tell if it works because I’m using DS.FixtureAdapter which seems to bypass transforms.

If anyone knows how to get the fixture adapter working with the JSON serializer, please hit the Reply as new Topic link and let me know! :grinning: