Skip to content

Instantly share code, notes, and snippets.

@zhubert
Created May 3, 2012 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhubert/2587401 to your computer and use it in GitHub Desktop.
Save zhubert/2587401 to your computer and use it in GitHub Desktop.
Ember Associations
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."
)
class CommentSerializer < ActiveModel::Serializer
attributes :body, :id, :post_id
end
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."
)
class PostSerializer < ActiveModel::Serializer
embed :ids, :include => true
attributes :title, :body, :id
has_many :comments
end
@zhubert
Copy link
Author

zhubert commented May 3, 2012

post = App.store.find(App.Post,1)
JSON.stringify(post)
"{"id":1,"title":"Hello","body":"world"}"

@zhubert
Copy link
Author

zhubert commented May 3, 2012

JSON.stringify(post.comments)
undefined
JSON.stringify(post.get('comments'))
TypeError: Converting circular structure to JSON

@zhubert
Copy link
Author

zhubert commented May 3, 2012

comment = App.store.find(App.Comment,1)
JSON.stringify(comment)
"{"id":1,"body":"wheeeee","post_id":1}"

@zhubert
Copy link
Author

zhubert commented May 3, 2012

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