如何将儿童记录添加到现有父记录? [英] How to Add Child Record to Existing Parent Record?

查看:122
本文介绍了如何将儿童记录添加到现有父记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在谷歌搜索和冲浪堆叠溢出有关这个问题的一些提示,但信息分散在最好。

I've been googling and scouring Stack Overflow for some sort of hint on this subject but the information is scattered at best.

我正在尝试创建一个新的儿童记录(评论)并将其保存到现有的父记录( Post )。我正在使用Ember-Model,而不是Ember-Data,但是任何提示或指针都将不胜感激。

I'm trying to Create a new Child Record (Comment) and save it to an existing Parent Record (Post). I am using Ember-Model, rather than Ember-Data, but any tips or pointers would be greatly appreciated.

目前,我已经成功地创建了一个新的嵌入评论,但使用新建 Post 记录。所以:

At the moment, I've been successful creating a new, embedded Comment but only when it is created with a new Post record. So:

如何加载/检索当前加载的 Post (父记录)订单申请评论(子记录)?

How do I go about loading/retrieving the currently loaded Post(parent record) in order to apply Comments (child records) to it?

我一直在阅读控制器依赖,使用需要: this.controllerFor this.modelFor 为了访问另一个控制器/模型的内容,但是无法将这些东西联系起来有意义。

I've been reading up on controller dependencies, using needs: and this.controllerFor and this.modelFor in order to have access to another controller/model's content but have been unable to wire these things together into something meaningful.

无论如何,这里是我将应用程序代码缩小到了,希望我可以绊倒正确的方式...

Anyway, here is what I've whittled my application code down to, in the hopes I might be able to stumble into the proper way of doing this...

    App.Router.map(function() {
      this.resource('post', { path: '/:post_id' }, function() {
        this.resource('comments', { path: '/comments'} );
        });
    });

我删除了所有其他资源&路线,所以我留下了 App.Post App.PostIndex 应用程序。点评。我认为我的路线是这里的问题,我认为我没有正确地执行在我的评论 Post 记录的方法c $ c> route。

I removed all the other resources & routes, so I'm left with App.Post, App.PostIndex, and App.Comments. I think my routes are the issue here, I assume I'm not properly implementing the methods to use the loaded Post record in my Comments route.

    App.IndexRoute = Ember.Route.extend({
      model: function() {
        return App.Post.find();
      },

      setupController: function(controller, model) {  // I'm not certain if this
        controller.set('content', model);           // setupController is needed?
      }

    });

    App.PostRoute = Ember.Route.extend({
      model: function(params) {
        return App.Post.find(params.post_id);
      },

      setupcontroller: function( controller, model) { // again, unsure if this
          this.controllerFor('post').get('comments'); // is correct.
            controller.set('content', comments);
      }

    });

    App.CommentsRoute = Ember.Route.extend({
      afterModel: function() {
          this.set('post', this.modelFor('post'));
        },

      setupcontroller: function( controller, model) {
          this.controllerFor('post').get('comments');
            controller.set('content', comments);
        }

    });



控制器



Controller

App.CommentsController = Ember.ArrayController.extend({
  needs: "post",

  actions: {

     addComment: function() {
          var post = App.Post.create({
            title: 'static post title'
          });

              post.get('comments').create({
                message: 'static message'
            });

              post.save();

      }

  }

});

这是我现在的评论控制器,可以创建一个新的 code>嵌入评论。我已经找到并提供了许多例子来创建评论,但没有一个似乎对我有用。基本上,我正在努力将 var post = ... 定义为当前加载的记录。我已经采取了各种方法试图试用错误。到目前为止,我试图:

This is my current Comments Controller, which can create a new Post with an embedded Comment. I've found and been given numerous examples in which to create the Comment, but none seem to work for me. Basically, I'm struggling with defining the var post = ... as the currently loaded record. I've implemented various approaches in an attempt at trial & error. Thus far I have attempted:


  • var post = App.Post.create() code>,返回属性undefined,因为这将创建一个新的记录。但是,我给了它一个镜头,我看到的每个例子都是这样定义他们的记录。

  • var post = App.Post.create();, returns property undefined, as this would create a new record. However, I gave it a shot as every example i saw related to this defined their record as such.

var post = this。 get('post'); ,返回一个不能在未定义的情况下调用get。我已经尝试使用这种方法来定义我的当前帖子在评论控制器和发布控制器。

var post = this.get('post');, returns a cannot call 'get' on undefined. I've tried using this method of defining my current post on both the Comments controller and Post controller.

var post = this.get('controllers.post.content); 返回'循环错误'从我正在使用的后端。

var post = this.get('controllers.post.content);, returns a 'cyclic error' from the backend I'm using.

var post = App.Post.find(); ,返回一个不能在未定义的情况下调用'get'。

var post = App.Post.find();, returns a cannot call 'get' on undefined.

var post = App.Post.find(1); ,再次,返回一个不能在未定义的时候调用get。我想给它一个镜头,因为这是人们提供的重复的例子之一。我使用的后端对每个记录应用自己的ID,我不知道我是否能够/如何使用 .find()方法使用动态ID价值并仅检索我刚刚加载的模型。

var post = App.Post.find(1);, Again, returns a cannot call 'get' on undefined. Figured I'd give it a shot because this is one of those recurring examples people provide. The backend I use applies its own ID to each record, and I'm unsure if I would be able to/how to have the .find() method use a dynamic ID value and retrieve only the model I just loaded.

我猜,我没有正确设置我的路线和控制器依赖关系?

I'm guessing that I'm not properly setting up my Routes and Controller dependencies?

如果有人有建议,相关链接或修复,我将非常感激。

If anyone has a suggestion, relevant link, or fix I would be very grateful.

这个(看似简单的)问题/用例在这一点上让我有目共睹。

This one (seemingly simple) issue/use case has me at wit's end at this point.

推荐答案

pre beta 2):

Try this (works pre beta 2):

App.CommentsController = Ember.ArrayController.extend({
  actions: {
     addComment: function() {
        this.content.createRecord({
            message: 'static message'
        });   
     }
  }
});

Ember Data Beta 2及更高版本:

Ember Data Beta 2 and later:

App.CommentsController = Ember.ArrayController.extend({
  needs: ["post"],
  actions: {
     addComment: function() {
        var post = this.get('controllers.post');
        var comment = this.get('store').createRecord('comment', {
            message: 'static message',
            post: post
        }); 
        comment.save().then(function() {
            post.addObject(comment);
            // You may or may not need to save your post, too. In my case my backend handles 
            // the inverses of relationships (if existing), so there's no need. We still need 
            // to do this for Ember, though

        });      
     }
  }
});

这篇关于如何将儿童记录添加到现有父记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆