Created
May 3, 2012 17:25
-
-
Save zhubert/2587401 to your computer and use it in GitHub Desktop.
Ember Associations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.Comment = DS.Model.extend( | |
body: DS.attr("string") | |
post: DS.belongsTo('App.Post') | |
validate: -> | |
if @get("body") is `undefined` or @get("body") is "" | |
"Comments require a body." | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CommentSerializer < ActiveModel::Serializer | |
attributes :body, :id, :post_id | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.Post = DS.Model.extend( | |
title: DS.attr("string") | |
body: DS.attr("string") | |
comments: DS.hasMany('App.Comment') | |
validate: -> | |
if @get("title") is `undefined` or @get("title") is "" or @get("body") is `undefined` or @get("body") is "" | |
"Posts require a title and body." | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostSerializer < ActiveModel::Serializer | |
embed :ids, :include => true | |
attributes :title, :body, :id | |
has_many :comments | |
end |
JSON.stringify(post.comments)
undefined
JSON.stringify(post.get('comments'))
TypeError: Converting circular structure to JSON
comment = App.store.find(App.Comment,1)
JSON.stringify(comment)
"{"id":1,"body":"wheeeee","post_id":1}"
Ahh. So this is the syntax
JSON.stringify(post.get('comments').objectAt(0))
"{"id":1,"body":"wheeeee","post_id":1}"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
post = App.store.find(App.Post,1)
JSON.stringify(post)
"{"id":1,"title":"Hello","body":"world"}"