使用Ember.js推送属于父模型的数据

使用Ember.js推送属于父模型的数据,ember.js,ember-data,Ember.js,Ember Data,接下来,场景与概述的场景相同,即一个帖子列表,每个帖子都有零条或多条评论 假设有一个路由器 App.Router.map(function() { this.resource('posts', function() { this.resource('post', { path: '/:post_id' }, function() { this.resource('comments', function() { this.route('create');

接下来,场景与概述的场景相同,即一个帖子列表,每个帖子都有零条或多条评论

假设有一个路由器

App.Router.map(function() {
  this.resource('posts', function() {
    this.resource('post', { path: '/:post_id' }, function() {
      this.resource('comments', function() {
        this.route('create');
      });
    });
  });
});
post的模型如下所示

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  comments: DS.hasMany('comment', { async: true })
});
{
  "post": {
    "id": 1,
    "title": "Rails is omakase",
    "links": { "comments": "/posts/1/comments" }
  }
}
例如,一篇文章就是下面的一篇

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  comments: DS.hasMany('comment', { async: true })
});
{
  "post": {
    "id": 1,
    "title": "Rails is omakase",
    "links": { "comments": "/posts/1/comments" }
  }
}
通过这种方式,我可以使用以下路径加载此帖子的评论

App.CommentsRoute = Ember.Route.extend({
  model: function() {
    return this.modelFor('post').get('comments');
  }
});
现在的问题是:如何创建CommentsCreateRoute,以便新的评论实际上发布到/posts/1/comments而不是/comments

到目前为止,我有以下几点

App.CommentsCreateRoute = Ember.Route.extend({
  model: function() {
    var newComment = this.store.createRecord('comment');
    newComment.set('title', 'test comment');
    return newComment;
  }
});

App.CommentsCreateController = Ember.ObjectController.extend({
  actions: {        
    saveEditing: function(){
      var newComment = this.get('model');
      newComment.save().then(function(comment) {
        /* OK saved */
      }, function(response) {
        /* show errors */
      });
    }
  }
});
这个休息计划有意义吗

GET /posts/{post_id}/comments
PUT /posts/{post_id}/comments/{comment_id}
POST /posts/{post_id}/comments
或者我应该使用以下命令

GET /posts/{post_id}/comments
PUT /comments/{comment_id}
POST /comments

还是别的?哪个是最标准的?

检查一下:您可以覆盖您的通信适配器,因此App.CommentAdapter=DS.RESTAdapter.extend{pathForType:functiontype{return'/posts/1/comments';//因为这需要是动态的,所以您需要实现一些逻辑来生成这个};我考虑过了,但是函数只得到参数类型。我从哪里提取1?我会继续为你调查。。。但对我来说,你想这样做实际上没有多大意义。。。因为现在您正在前端动态生成需要在后端进行说明的路由。发布到动态URL的理由是什么?每个帖子都有一个GET的评论链接,所以我认为最常用的方法是在帖子中使用相同的URL。我相应地更新了问题的结尾。显然,我应该选择我发布的第二个休息计划,谢谢。