How to use transform?


I’m using ember-cli for my app and I want to create a custom attribute. I create a custom transform now but it isn’t called.

What I do is following this page to create an ArrayTranform. I don’t call App.register() since I think ember-cli handles that. I debug and check that the transformed is registered by using WebApp.container.lookup(‘transform:array’). It’s not called since I add breakpoints to serialize() and deserialized() but neither is called. There’s data back from the server when I use store to find the model but the model is null.

Am I missing any dumb things? I don’t find many pages online about transform especially with ember-cli. Thanks.

I don’t call App.register() since I think ember-cli handles that

I’ve never used custom transforms within ember-cli, however I would say you could validate this pretty quickly to see if ember-cli is in fact able to lookup these transforms. It /should/ work just fine assuming you have a transforms folder on the root-level of your application.

Anywhere in the context of your application, run this.container.lookup('transform:array'); and if nothing is returned you’ll need to add a custom initializer to register these transforms. If I have time tomorrow I’ll try and replicate this issue and see if I can come up with a fix.

However, I suggest you take a look at model-fragments which solve this primitive array story pretty nicely.

I tried WebApp.container.lookup(‘transform:array’) in FireBug and it returns an object. I assume that the transform is registered. But the problem then is that it’s not called. I look at RESTSerializer and don’t see how it calls transform.

Any suggestion how to debug this issue? why the transform isn’t used?

I think I know what’s happening. the model hook in the route may get a different format of JSON. I will work on that.

It’s indeed that model hook expect an array of object but JSON returned is an object.

But there’s a new problem. I can see that the object is created in model hook. But in afterModel, when I try to get resolved model, it’s undefined. I don’t know why.

I figured out. The JSON I returned, the model that’s created, and the controller (ArrayController vs ObjectController) should be for the same type: either array or object. And I add a then() to store.find() in the model hook. I should return the the model in then().