This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Simple example

  • Loading branch information...
bergie committed Feb 28, 2012
1 parent 939e01c commit cf7faeb994bd1b688be314a4aec457395cbcaad9
@@ -0,0 +1,51 @@
+{Schema} = require 'jugglingdb'
+express = require 'express'
+resource = require 'express-resource'
+resourceJuggling = require "#{__dirname}/../../lib/resource-juggling"
+
+# We use Redis in this example
+schema = new Schema 'redis', {}
+
+# Define our model(s)
+Post = schema.define 'Post'
+ title:
+ type: String
+ length: 255
+ content:
+ type: Schema.text
+
+Comment = schema.define 'Comment',
+ author:
+ type: String
+ content:
+ type: Schema.text
+
+# Define some relationships
+Post.hasMany Comment,
+ as: 'comments'
+ foreignKey: 'postId'
+
+Comment.belongsTo Post,
+ as: 'post'
+ foreignKey: 'postId'
+
+app = express.createServer()
+app.use require('connect-conneg').acceptedTypes
+app.set 'view engine', 'jade'
+app.set 'views', "#{__dirname}/views"
+app.listen 8080
+
+# Posts are on top level
+posts = app.resource resourceJuggling.getResource
+ schema: schema
+ name: 'Post'
+ model: Post
+
+# Comments are under posts, so we use Post.comments as the collection
+comments = app.resource 'comments', resourceJuggling.getResource
+ schema: schema
+ name: 'Comment'
+ model: Comment
+ collection: (request) ->
+ request.Post.comments
+posts.add comments
@@ -0,0 +1,2 @@
+div
+ !=partial("show", items)
@@ -0,0 +1,3 @@
+div
+ h2 !{item.author}
+ !{item.content}
@@ -0,0 +1,3 @@
+div(about='/', typeof='blog')
+ div(rel='posts', rev='blog')
+ !=partial("show", items)
@@ -0,0 +1,8 @@
+- var url = '/' + item.id;
+- var commentsUrl = url + '/comments';
+article
+ h1
+ a(href=url) !{item.title}
+ div !{item.content}
+ div
+ a(href=commentsUrl) Comments
@@ -0,0 +1,11 @@
+!!! 5
+html
+ head
+ title Post and Comments example
+ body
+ div.container
+ div.content
+ div.page_header
+ h1 Post and comments example
+ !{body}
+

0 comments on commit cf7faeb

Please sign in to comment.