儿童记录和余烬模型 [英] child records and ember model

查看:111
本文介绍了儿童记录和余烬模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[更新]



感谢您的帮助。



我已经能够创建具有静态数据的嵌入式注释记录的新的Post记录。通过这种方法:

  App.CommentsController = Ember.ArrayController.extend({
需要:post,

动作:{

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

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

post.save();

}

}

});

但是我不清楚如何通过评论控制器或路由按顺序检索当前帖子在已经存在的帖子上创建评论记录。



在搜索和筛选随机SO和文章后,我尝试了一系列方法来调用当前帖子,但仍然没有骰子。



我已经尝试将 var post = App.Post.create 设置为:

  var post = App.Post.find(),
var post = this.get('controllers.post.model') ,
var post = this.get('controllers.post.content'),
var post = this.get('controllers.post'),
var post = this.get 'post'),

我还设置了我的 CommentsController 需要需要:post



我还尝试添加:

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

我阅读了一条说明行动应该在路线中定义的部分,我需要定义我的 addComment 函数在 PostRoute



m想知道我是否需要使用 var post = App.Post.find(post)或某种性质的东西。



<我在正确的轨道上吗?感觉就像我在黑暗中在扫帚衣柜里的大象拍摄,仍然失踪...

解决方案

尝试这个



b







$ post.save()。then(function(post){self.transitionToRoute('comment.create',record);});

然后在评论控制器中

  var post = this.get('comment')。create(); 


[Update]

Thanks for the help so far.

I've been able to create a new Post record with an embedded Comment record with static data. Via this method:

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

      actions: {

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

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

                  post.save();

          }

      }

    });

But I'm falling short figuring out how to retrieve the current Post via the CommentsController or Route in order to create Comment records on an already existing Post.

After searching and sifting through random SOs and articles, I've tried an array of methods to call the current Post but still no dice.

I've tried setting var post = App.Post.create to:

var post = App.Post.find(), 
var post = this.get('controllers.post.model'),
var post = this.get('controllers.post.content'),
var post = this.get('controllers.post'),
var post = this.get('post'),

I've also set my CommentsController to have needs: "post".

I've also tried adding:

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

I read a piece that stated actions should be defined in the routes as well, would I need to define my addComment function in the PostRoute?

I'm wondering if I need to use var post = App.Post.find(post) or something of that nature.

Am I on the right track here? Feels like I'm taking shots in the dark at an elephant in a broom closet and still missing...

解决方案

try this

in the post controller when you save the record use this

post.save().then(function(post)     {self.transitionToRoute('comment.create', record);});

then in comments controller

 var post = this.get('comment').create();

这篇关于儿童记录和余烬模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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