Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No container in JSONSerializer #1741

Closed
gonvaled opened this issue Feb 12, 2014 · 10 comments
Closed

No container in JSONSerializer #1741

gonvaled opened this issue Feb 12, 2014 · 10 comments

Comments

@gonvaled
Copy link

The container is undefined. Custom transforms are not working. This is probably related to pull request #1162.

Also, there is no explicit test for transformFor.

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/json_serializer.js#L736:

  transformFor: function(attributeType, skipAssertion) {
    var transform = this.container.lookup('transform:' + attributeType);
    Ember.assert("Unable to find transform for '" + attributeType + "'", skipAssertion || !!transform);
    return transform;
  }
@gonvaled
Copy link
Author

And this is the reason why it is not cached during normal testing:

https://github.com/emberjs/data/blob/master/tests/ember_configuration.js

    // Prevent all tests involving serialization to require a container
    DS.JSONSerializer.reopen({
      transformFor: function(attributeType) {
        return this._super(attributeType) || transforms[attributeType];
      }
    });

@bradleypriest
Copy link
Member

@gonvaled what version of ED are you using and in what situation is this happening?
Transforms are working for me and everything has the container.
How are you instantiating the Serializer?

@gonvaled
Copy link
Author

Currently (painfully!) migrating to ember-data 1.0.0-beta.6. My serializer is setup in a similar way to what I was doing for ember-data 0.0.14:

SettingsApp.MetaRESTSerializer = DS.RESTSerializer.extend({
    primaryKey: '_id',
    extractMeta: function(store, type, payload) {
        // TODO: standard implementation. Check with backend to implement this for good
        if (payload && payload.meta) {
            store.metaForType(type, payload.meta);
            delete payload.meta;
        }
    },

});

And:

SettingsApp.ApplicationAdapter = DS.RESTAdapter.extend({
    bulkCommit: false,
    namespace: SettingsApp.config.API_NAMESPACE,
    host: SettingsApp.apiUrl,
    serializer: SettingsApp.MetaRESTSerializer.create(),
    ajax: function(url, type, hash) {
        var ajaxPromise = this._super(url, type, hash);
        if (DEBUG) { console.log('>>>>> REQUEST > %s:%s > hash=%o ajaxPromise=%o', type, url, hash, ajaxPromise); }
        ajaxPromise.then(function(json){
            if (DEBUG) { console.log('>>>>> REPLY   > %s:%s > %o', type, url, json); }
        });
        return ajaxPromise;
    }
});

It could be that I am missing something when setting up the serializer. In that case, that should be clarified in the migration guide.

@bradleypriest
Copy link
Member

No, that looks good, when/how does it explode?

@gonvaled
Copy link
Author

As soon as the first model is going to be serialized, with transform "string" (one of the standard transforms). I am trying to force the container manually on the serializer, to see what happens.

Update: it worked:

SettingsApp.MetaRESTSerializer = DS.RESTSerializer.extend({
    primaryKey: '_id',
    extractMeta: function(store, type, payload) {
        // TODO: standard implementation. Check with backend to implement this for good
        if (payload && payload.meta) {
            store.metaForType(type, payload.meta);
            delete payload.meta;
        }
    },
    container: SettingsApp.__container__,
});

@sly7-7
Copy link
Contributor

sly7-7 commented Feb 12, 2014

I don't think it's correct to instantiate your serializer as this. I think it should be looked up via the container too.
I would try to do serializer: 'metaREST' or something like that

Or maybe you can just do

SettingsApp.ApplicationSerialzer = SettingsApp.MetaRESTSerializer and don't define it in the Adapter declaration

@bradleypriest
Copy link
Member

Similar to what @sly7-7 said, how are you registering the MetaRESTSerializer as your application's serializer?

@gonvaled
Copy link
Author

@sly7-7: do you have an example on how to do that? That was working for 0.0.14, and I see nothing in the transition guide regarding changes on the instantiation of the serializer.

@bradleypriest : as you can see in ApplicationAdapter.serializer

Is there any complete example on how to use ember-data, including serializers/adapter/models.

@bradleypriest
Copy link
Member

Ok, so you need to remove the "serializer" declaration from the adapter and define an App.ApplicationSerializer = MetaRESTSerializer instead.
1.0 moved to containerizing everything.

The transition doc has all the basic upgrading stuff, and the Models section of the official docs has some info.

We're still lacking a bit so if there's parts missing, we'd love for you to help out.

I'm gonna close this as it doesn't look like a bug, ping me if that doesn't work

@gonvaled
Copy link
Author

That is solving the issue, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants