まず、IRC を手伝ってくれた Mike Grassotti に感謝します。彼は save メソッドでバグを解決するのを手伝ってくれました。
私の問題は、コンソールでレコードが作成されているのに表示されないことです。
ember-data を使用して新しいレコードを作成しています。addComment関数はトランザクション内にレコードを 作成しますが、save 関数はthis.transaction.commitのみを呼び出します。
save() をクリックした後のコンソールでは、レコードが作成されたように見えますが、ハンドルバーには新しく作成されたレコードが表示されません。これは、console.log の結果を掘り下げたときにコンソールに表示される内容の抜粋です。
>committed: Object
firstRecordKind: "belongsTo"
firstRecordName: "post"
>firstRecordReference: Object
clientId: 4
id: "ember411"
>type: EmBlog.Comment
ClassMixin: Ember.Mixin
>FIXTURES: Array[1]
0: Object
body: "ty"
id: "ember411"
post: "1"
新しいレコードを作成するには、[投稿] -> [投稿タイトル] -> 一番下にある [コメントを追加] -> [保存] の順にクリックすると、レコードが作成されていないことがわかります。
jsfiddleからの関連するコードのビット。このコントローラーはサイドロードされるため、ルートがありません
EmBlog.CommentNewController = Em.ObjectController.extend({
needs: ['postsShow'],
isAddingNew: false,
addComment: function(body){
console.log("this: ", this.toString());
var post = this.get('controllers.postsShow.content');
console.log(post);
transaction = this.get('store').transaction();
console.log(transaction);
console.log(this.get('content').toString());
this.set('content', transaction.createRecord(EmBlog.Comment, ({post: post })));
this.transaction = transaction;
console.log(this.get('content').toString());
this.set('isAddingNew', true);
},
save: function(){
var comment = this.get('content');
comment.one('didCreate', this, function() {
this.set('isAddingNew', false);
});
this.transaction.commit();
}
});
ハンドルバー テンプレートからの関連ビット
<script type="text/x-handlebars" data-template-name="posts/show">
<h1>Post</h1>
<h3> {{title}} </h3>
<h3> {{body}} </h3>
<br/>
<p> {{#linkTo 'posts.index'}} back {{/linkTo}}</p>
<p> {{#linkTo 'posts.edit' content}} Edit the post {{/linkTo}}</p>
<br/>
<b> Comments</b>
{{render 'comment/new' comments}}
</script>
<script type='text/x-handlebars' data-template-name='comment/new'>
{{#if controller.isAddingNew}}
<form {{action save on='submit'}}>
{{view Ember.TextArea valueBinding="body" placeholder="body"}}
<button type="submit"> save comment </button>
</form>
{{/if}}
<br/>
<div>
<button {{action addComment}} {{bindAttr disabled="isAddingNew"}}>Add Comment</button>
</div>
</script>
ありがとう