3
votes

I've a simple app that has posts and comments.

If I create a new comment within a post and save it: Ember's default behaviour is to post the new Comment as JSON to "/comment".

I like to have it posted to "/posts/38/comment" and thought about I overwriting the buildURL-method of a Model-Specific RESTadapter to change the URL.

But the buildURL methods signature is only (type, id). Type is just a string of the model name an id undefined for a new comment.

So how can I access the "to be saved" model's content? Or is there another poper way to get the post-id to build the URL? In this example - How to get access to the number 38 within buildURL.

Or am I on the wrong path and there is a better way to archive it?

1

1 Answers

1
votes

Define an App.Comment-specific adapter, and customise the createRecord method there:

App.CommentAdapter = DS.RESTAdapter.extend({
  createRecord: function(store, type, record) {
    var data = {};
    var serializer = store.serializerFor(type.typeKey);

    serializer.serializeIntoHash(data, type, record, { includeId: true });

    // Custom stuff
    var postId = record.get('post.id');
    var url = this.buildURL(type.typeKey, postId) + '/comment';
    return this.ajax(url, "POST", { data: data });
  }
});