mardi 28 août 2018

Inject Relationships into Ember Payload

i have a component that shows and creates comments to say a post, this component has a form for create the new comments and send them via POST to the backend, the normal payload would be:

{data: {
    attributes: {
        created_at: "foo",
        autor: "foo",
        text: "foo"
    },
    relationships: {
        post: {
            data: { type: "posts", "id": 1234 },
            id: "1234",
            type: "loans"
            }
     }
     type: "comment"
     }
}

The problem comes when you need to use the component in another view and more important when the name of the model is different, to say posts_breakdown, in this case the payload would be:

{data: {
    attributes: {
        created_at: "foo",
        autor: "foo",
        text: "foo"
        },
    relationships: {
        post: {
            data: null
            }
        }
     type: "comment"
    }
}

Clearly, in comments there is no relation posts_breakdown, the first thing that I tried to add this relation to the model with "posts_breakdown: belongsTo (posts_breakdown)" the problem is, that the backend cann't recognize it and is not possible to modify it. The backend, is taking the values on the relationships to relate the comment with the post (post_id field into comment table)

My question: There is some way to "trick" the backend and / or modify the payload, so think that the post_breakdown model is post?

Below is a representation of how I have the defined models:

comment.js:
    export default DS.Model.extend ({
        author: DS.attr (),
        text: DS.attr (),
        created_at: DS.attr (),
        post: DS.belongsTo ('post'),
        posts_breakdown: DS.belongsTo ('posts_breakdown'),
    });

posts.js:
    export default DS.Model.extend ({
        text: DS.attr (),
        created_at: DS.attr (),
        author: DS.attr (),
        comments: DS.hasMany ('comments'),
    });

post_breakdown.js
    export default DS.Model.extend ({
        most_commented_post: DS.attr (),
        last_commented_post: DS.attr (),
        frequent_users: DS.attr (),
        comments: DS.hasMany ('comments'),
    });




Aucun commentaire:

Enregistrer un commentaire