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

BelongsTo relation called 'type' in combination with a link returns error #3784

Closed
jevanlingen opened this issue Sep 18, 2015 · 7 comments
Closed

Comments

@jevanlingen
Copy link

Today I was updating Ember Data from 1.13.8 to 1.13.13. As #3725, the type attribute can be used again. My case is a little different, as I have a belongs-to relation. This still shouldn't be a problem as @tschoartschi in ticket #3769 also has a type belongs-to relation and the fix worked for him.

I got following file model:

export default DS.Model.extend({
    name: attr(),
    size: attr(),
    lastModified: attr(),
    created: attr(),

    directory: DS.belongsTo('directory', { async: true }),
    type: DS.belongsTo('file-type', { async: true })
}

The type property belongs to a file-type model, which is loaded async with an own given link. So I made a file serializer:

export default DS.RESTSerializer.extend({
    isNewSerializerAPI: true,

    normalize(typeClass, hash, prop) {
        hash.links = {
            type: `link-to-another-system?type_id=${hash.type}`
        };

        return this._super(...arguments);
    }
});

Yet, I still seem to face a problem regarding the type property. Pull #3725 just fixes the problem for attributes (by adding the primaryHasTypeAttribute variable).

Maybe my own serializer is the problem, but

var primaryHasTypeAttribute = get(modelClass, 'attributes').get('type');

returns undefined for me. I find that logicly, as a belong-to relation is not a attribute (to be honest I don't understand how it's possible pull #3725 fixes #3769).

Whaterever the case. if a set primaryHasTypeAttribute to true by hand, the problem is solved! Maybe the check should also take relation properties into account?

@jevanlingen
Copy link
Author

Nobody?

@pangratz
Copy link
Member

pangratz commented Oct 9, 2015

@jevanlingen can you confirm that #3814 solves your issue, so this one can be closed?

@jevanlingen
Copy link
Author

@pangratz Yeah, I can confirm #3814 does fix this issue. Added the get(modelClass, 'relationshipsByName').get('type');line to my local code, problem was fixed!

@pangratz
Copy link
Member

pangratz commented Oct 9, 2015

won der ful! Good to hear! ✨

@jevanlingen
Copy link
Author

I still face one problem with the type atrribute. When I tested this back in october, I just checked if no errors occured.

But today I noticed the related link does not work. The serializer should return something like this for an async relation according to the JSON API spec:

data: {
  id: "something",
  ... // other props
  relationships: {
    type: {
      links: {
        related: "link-to-another-system"
      }
    }
  }
}

For every other attribute name the JSONSerializer does just that! But in case the type attribute is used, the JSONSerializer returns:

data: {
  id: "something",
  ... // other props
  relationships: {
    type: {
      date: {
        id: "something",
        type: "associated-related-model" //in my case 'file-type'
      }
    }
  }
}

This is plainly wrong, the JSONSerializer ignores the links property!

@jevanlingen jevanlingen reopened this Mar 25, 2016
@pangratz
Copy link
Member

Can you create an ember-twiddle demonstrating the issue you're having? You can use this as a starting point...

@jevanlingen
Copy link
Author

I was wrong about this. After creating the Twiddle, I understood my problem was of a different kind. The type belongsTo relationship did work in itself, it was another issue i ran into. See #4292!

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

No branches or pull requests

2 participants