Skip to content

Commit

Permalink
use DS.Serializer to send post_id with comments
Browse files Browse the repository at this point in the history
comments loaded async in post model
  • Loading branch information
ianwdunlop committed Oct 28, 2013
1 parent df896ef commit 0a98d99
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ App.CommentsNewController=Ember.ObjectController.extend({
save: function() {
var me = this;
var post = this.get('controllers.post.content');
var comment = this.get('store').createRecord('comment', { post_id: post.id, text: this.get('text') });
var comment = this.get('store').createRecord('comment', { post: post, text: this.get('text') });
comment.save().then(function(comment){
me.get('target').transitionTo('post.index');
});
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/models/post.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
App.Post=DS.Model.extend({
comments: DS.hasMany('comment'),
comments: DS.hasMany('comment', { async: true }),
title: DS.attr('string'),
text: DS.attr('string')
});
1 change: 1 addition & 0 deletions app/assets/javascripts/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
// return promise;
// }
//});
App.ApplicationSerializer = DS.ActiveModelSerializer.extend({});
2 changes: 1 addition & 1 deletion app/assets/javascripts/templates/post/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{{text}}
<ul>
{{#each comment in comments}}
<li>{{#linkTo post.comment comment}}{{comment.text}}{{/linkTo}}</li>
<li>{{#linkTo 'post.comment' comment}}{{comment.text}}{{/linkTo}}</li>
{{/each}}
</ul>
{{#linkTo 'comments.new'}}New Comment{{/linkTo}}
6 changes: 3 additions & 3 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class Post < ActiveRecord::Base
attr_accessible :text, :title
has_many :comments

# def as_json(options={})
# { :id => self.id, :title => self.title, :text => self.text, :comment_ids =>self.comments.collect{|comment| comment.id} }
# end
def as_json(options={})
{ :id => self.id, :title => self.title, :text => self.text, :comment_ids =>self.comments.collect{|comment| comment.id} }
end

end

0 comments on commit 0a98d99

Please sign in to comment.