Skip to content
Browse files

added deed model, REST and list

  • Loading branch information...
1 parent 96364a6 commit 69abe028b78d2f39c1fd76629546e5acc72d905e @rashfael committed Oct 27, 2012
View
12 app/controllers/deeds_controller.coffee
@@ -1,8 +1,14 @@
Controller = require 'controllers/base/controller'
DeedsPageView = require 'views/deeds_views'
+{Deed, Deeds} = require 'models/deeds'
module.exports = class DeedsController extends Controller
- historyURL: 'deeds'
+ historyURL: 'deeds'
- index: ->
- @view = new DeedsPageView()
+ index: ->
+ deeds = new Deeds()
+ deeds.fetch
+ success: (data) ->
+ console.log data
+ @view = new DeedsPageView
+ collection: deeds
View
9 app/models/deeds.coffee
@@ -0,0 +1,9 @@
+Model = require 'models/base/model'
+Collection = require 'models/base/collection'
+
+module.exports.Deed = class Deed extends Model
+ urlRoot: '/api/deeds'
+
+module.exports.Deeds = class Deeds extends Collection
+ model: Deed
+ url: '/api/deeds'
View
29 app/views/deeds_views.coffee
@@ -1,5 +1,30 @@
PageView = require 'views/base/page_view'
+CollectionView = require 'views/base/collection_view'
+View = require 'views/base/view'
+
+
+class DeedItemView extends View
+ template: require 'views/templates/deedItem'
+ tagName: 'tr'
+
+class DeedsCollectionView extends CollectionView
+ template: require 'views/templates/deedTable'
+ tagName: 'table'
+ className: 'table'
+ itemView: DeedItemView
+ listSelector: 'tbody'
+
module.exports = class DeedsPageView extends PageView
- template: require 'views/templates/deeds'
- className: 'deeds-page'
+ template: require 'views/templates/deeds'
+ className: 'deeds-page'
+
+ initialize: (options) =>
+ @deedsCollectionView = new DeedsCollectionView
+ collection: options.collection
+
+ render: () =>
+ super
+ @deedsCollectionView.container = @$ '#deeds'
+ @deedsCollectionView.render()
+ @deedsCollectionView.renderAllItems()
View
2 app/views/templates/deedItem.jade
@@ -0,0 +1,2 @@
+td= name
+td= description
View
5 app/views/templates/deedTable.jade
@@ -0,0 +1,5 @@
+thead
+ tr
+ th Name
+ th Description
+tbody
View
4 app/views/templates/deeds.jade
@@ -1,2 +1,4 @@
.page-header
- h1 Deeds
+ h1 Deeds
+
+#deeds
View
10 lib/routers/Crud.coffee
@@ -1,11 +1,11 @@
module.exports = class Crud
constructor: (app) ->
- app.get "/admin/api/#{@prefix}", @list
- app.post "/admin/api/#{@prefix}", @add
- app.get "/admin/api/#{@prefix}/:id", @item
- app.put "/admin/api/#{@prefix}/:id", @update
- app.delete "/admin/api/#{@prefix}/:id", @delete
+ app.get "/api/#{@prefix}", @list
+ app.post "/api/#{@prefix}", @add
+ app.get "/api/#{@prefix}/:id", @item
+ app.put "/api/#{@prefix}/:id", @update
+ app.delete "/api/#{@prefix}/:id", @delete
list: (req, res) =>
View
6 lib/routers/Deed.coffee
@@ -0,0 +1,6 @@
+Model = global.mongoose.model 'Deed'
+Crud = require './Crud'
+
+module.exports = class ModelRouter extends Crud
+ model: Model
+ prefix: 'deeds'
View
1 lib/schemas/Deed.coffee
@@ -4,5 +4,6 @@ Schema = mongoose.Schema
schema = new Schema
name: String
+ description: String
module.exports = schema
View
44 lib/server.coffee
@@ -1,5 +1,5 @@
log4js = require 'log4js'
-logger = log4js.getLogger 'my-project'
+logger = log4js.getLogger 'shackkarma'
logger.setLevel 'INFO'
express = require 'express'
@@ -10,10 +10,12 @@ app = module.exports = express()
# Load db stuff
mongoose = require 'mongoose'
-mongoose.connect 'mongodb://localhost/my-project'
+mongoose.connect 'mongodb://localhost/shackkarma'
global.mongoose = mongoose
-Project = mongoose.model 'Model', require('./schemas/Model'), 'models'
+# Project = mongoose.model 'Model', require('./schemas/Model'), 'models'
+
+Deed = mongoose.model 'Deed', require('./schemas/Deed'), 'deeds'
# Server config
@@ -22,7 +24,7 @@ app.configure ->
app.use express.methodOverride()
app.use express.cookieParser 'ponies'
app.use express.session()
- # app.use log4js.connectLogger log4js.getLogger 'my-project-access'
+ app.use log4js.connectLogger log4js.getLogger 'shackkarma-access'
app.use express.static __dirname + '/../public'
app.use app.router
@@ -32,26 +34,26 @@ app.configure 'development', ->
app.configure 'production', ->
app.use express.errorHandler()
-# admin routers
-ModelRouter = require './routers/Model'
+DeedRouter = require './routers/Deed'
+deedRouter = new DeedRouter app
# cheap user list
-users =
- root:
- username: 'root'
- password: 'password'
-
-app.post '/authenticate', (req, res) ->
- if req.body.user?
- user = users[req.body.user.username]
- if user? and user.password = req.body.user.password
- return res.json user
- res.send 404
- else
- if req.session?.user?
- return req.session.user
- res.send 401
+# users =
+# root:
+# username: 'root'
+# password: 'password'
+
+# app.post '/authenticate', (req, res) ->
+# if req.body.user?
+# user = users[req.body.user.username]
+# if user? and user.password = req.body.user.password
+# return res.json user
+# res.send 404
+# else
+# if req.session?.user?
+# return req.session.user
+# res.send 401
app.get '*', (req, res) ->
res.sendfile path.normalize __dirname + '/../public/index.html'

0 comments on commit 69abe02

Please sign in to comment.
Something went wrong with that request. Please try again.