Skip to content

Instantly share code, notes, and snippets.

@inossidabile
Created May 23, 2012 13:17
Show Gist options
  • Save inossidabile/2775191 to your computer and use it in GitHub Desktop.
Save inossidabile/2775191 to your computer and use it in GitHub Desktop.
Joosy / Blog / Resource
Post.find 2, (post) -> # Loads entity from /posts/2
post('title') # Returns 'Test post'
post('title', 'New and better title!') # Sets new title value
post('title') # Returns 'New and better title!'
post.reload -> # Reloads fields from server
post('title') # Returns 'Test post'
post.delete -> console.log 'Deleted!' # Deletes fetched entity (DELETE /posts/2)
# If your field has a hash as a value you can use extended get/set notation
post('foo.bar') # Will get foo['bar'] value
post('foo.bar', '123') # Will set foo['bar'] value
Post.find 'all', (posts) ->
posts.size() # Number of retrieved posts
posts.at(0) # Get by index
posts.remove(0) # Remove first post
posts.remove(post) # Remove post by === comparison
posts.add(post) # Add post to the end of set
posts.add(post, 2) # Add post to specified index
posts.find (x) -> x.id() == '1' # Search with a callback
posts.findById(1) # Search by an ID
posts.each(post) -> # Iterate over all posts
console.log post
# Reload the whole set
posts.load [{id: 1, title: 't'}, {id: 2, title: 't2'}]
posts.reload() # Refetch collection from server
# This will run every time post changed
post.bind 'changed', -> console.log "#{post.id()} changed!"
# This will run when the post has changed for the first time
post.wait 'changed', -> console.log "#{post.id()} changed!"
# This will create empty resource with ID=2
post = Post.build 2
post('title') # Returns `undefined`
# This will create new resource with undefined id but having `title` field
post = Post.build title: 't' # This will create new resource without ID
post.id() # Returns `undefined`
post('title') # Returns 't'
# Note that Joosy has Identity map!
# `post` and `postCopy` will share same instance!
Post.find 1, (post) ->
postCopy = Post.build 1
postCopy('title') # Returns 'Hey, welcome to the joosy blog example'
Post.find 2, {from: 'foo'}, (post) -> # Loads entity from /posts/2/foo
Post.find 2, {parent: firstPost}, (post) -> # Loads entity from /posts/1/posts/2
Post.find 2, {params: {foo: 'bar'}}, (post) -> # Loads entity from /posts/2?foo=bar
rails g joosy:resource blog/comment
class @Post extends Joosy.Resource.REST
@map 'comments'
@map 'topComment', Comment
# {
# comments: [{id: 1, title: 'test'}],
# topComment: {id: 1, title: 'test'}
# }
Post.find 1, (post) ->
post('comments') # Collection of Comments
post('topComment') # Popular comment resource
post('topComment.title') # Returns 'test'
class @Post extends Joosy.Resource.REST
@beforeLoad (data) ->
data.date = new Date(data.date)
data
post = Post.build 1
Post.get (data) -> # GET /posts/
Post.put {from: 'foo'}, (data) -> # PUT /posts/foo
Post.post {parent: post}, (data) -> # POST /posts/1/posts
Post.delete {params: {foo: 'bar'}} # DELETE /posts/?foo=bar
post.get (data) -> # GET /posts/1
post.put {parent: post}, (data) -> # PUT /posts/1/posts/1
post.post {params: {foo: 'bar'}} # POST /posts/1 (foo: bar)
post.delete # DELETE /posts/1
class @Comment extends Joosy.Resource.REST
# Entity name for internal use
@entity 'comment'
# Base URL
@source '/foo'
# Set the field containing primary key
@primaryKey 'id'
# Sets the class that will be used to wrap collections
@collection CommentsCollection
@sbc
Copy link

sbc commented Jun 10, 2012

Lines 3 and 5 in Basic Entry Actions return 'Test post' based on current db seed.

@chatman-media
Copy link

Where I should paste it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment