Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

create deeds and display, change api to sockets

  • Loading branch information...
commit d48895c2aa645bd6a8b37b50d0b07e7537bab452 1 parent 69abe02
@rashfael authored
Showing with 407 additions and 332 deletions.
  1. +21 −13 app/application.coffee
  2. +1 −0  app/assets/index.html
  3. +45 −0 app/controllers/authentication_controller.coffee
  4. +25 −3 app/controllers/deeds_controller.coffee
  5. +0 −46 app/controllers/session_controller.coffee
  6. +29 −0 app/lib/iosync.coffee
  7. +3 −2 app/models/deeds.coffee
  8. +2 −0  app/routes.coffee
  9. +28 −7 app/views/deeds_views.coffee
  10. +22 −10 app/views/header_view.coffee
  11. +7 −3 app/views/login_view.coffee
  12. +3 −0  app/views/styles/application.styl
  13. +12 −0 app/views/templates/deed_add.jade
  14. +3 −0  app/views/templates/deed_item.jade
  15. 0  app/views/templates/{deedItem.jade → deed_list_item.jade}
  16. +1 −0  app/views/templates/deeds.jade
  17. +3 −3 app/views/templates/header.jade
  18. +8 −1 app/views/templates/login.jade
  19. +98 −8 app/views/templates/mixins/form_helpers.jade
  20. +0 −5 generators/collection/collection.coffee.hbs
  21. +0 −11 generators/collection/generator.json
  22. +0 −12 generators/collection_test/collection_test.coffee.hbs
  23. +0 −9 generators/collection_test/generator.json
  24. +0 −5 generators/collection_view/collection_view.coffee.hbs
  25. +0 −9 generators/collection_view/generator.json
  26. +0 −3  generators/controller/controller.coffee.hbs
  27. +0 −11 generators/controller/generator.json
  28. +0 −5 generators/controller_test/controller_test.coffee.hbs
  29. +0 −9 generators/controller_test/generator.json
  30. 0  generators/generator/generated_file.coffee.hbs
  31. +0 −13 generators/generator/generator.json
  32. +0 −9 generators/generator/generator.json.hbs
  33. +0 −11 generators/model/generator.json
  34. +0 −3  generators/model/model.coffee.hbs
  35. +0 −9 generators/model_test/generator.json
  36. +0 −5 generators/model_test/model_test.coffee.hbs
  37. +0 −9 generators/page_view/generator.json
  38. +0 −5 generators/page_view/page_view.coffee.hbs
  39. +0 −9 generators/style/generator.json
  40. +0 −1  generators/style/style.styl.hbs
  41. +0 −9 generators/template/generator.json
  42. 0  generators/template/template.hbs.hbs
  43. +0 −11 generators/view/generator.json
  44. +0 −5 generators/view/view.coffee.hbs
  45. +0 −9 generators/view_test/generator.json
  46. +0 −5 generators/view_test/view_test.coffee.hbs
  47. +0 −1  lib/routers/Crud.coffee
  48. +2 −2 lib/routers/Deed.coffee
  49. +6 −8 lib/routers/IoCrud.coffee
  50. +1 −1  lib/schemas/Deed.coffee
  51. +12 −0 lib/schemas/User.coffee
  52. +73 −31 lib/server.coffee
  53. +2 −1  package.json
View
34 app/application.coffee
@@ -1,16 +1,20 @@
Chaplin = require 'chaplin'
+Router = require 'chaplin/lib/router'
mediator = require 'mediator'
routes = require 'routes'
-SessionController = require 'controllers/session_controller'
+AuthenticationController = require 'controllers/authentication_controller'
HeaderController = require 'controllers/header_controller'
FooterController = require 'controllers/footer_controller'
Layout = require 'views/layout'
+require 'lib/iosync'
+
# The application object
module.exports = class Application extends Chaplin.Application
# Set your application name here so the document title is set to
# “Controller title – Site title” (see Layout#adjustTitle)
title: 'Shack Karma'
+ serverUrl: 'http://localhost:9000/'
initialize: ->
super
@@ -19,19 +23,18 @@ module.exports = class Application extends Chaplin.Application
@initDispatcher()
@initLayout()
@initMediator()
+ #@initRouter()
+ @router = new Router()
+ @initSocket ->
+ auth = new AuthenticationController()
- # Application-specific scaffold
- @initControllers()
-
- # Register all routes and start routing
- @initRouter routes
- # You might pass Router/History options as the second parameter.
- # Chaplin enables pushState per default and Backbone uses / as
- # the root per default. You might change that in the options
- # if necessary:
- # @initRouter routes, pushState: false, root: '/subdir/'
+ mediator.subscribe '!auth:success', =>
+ @initControllers()
+ # register routes late
+ routes @router.match
+ @router.startHistory()
+ #mediator.publish '!router:route', ''
- # Freeze the application instance to prevent further changes
Object.freeze? this
# Override standard layout initializer
@@ -61,4 +64,9 @@ module.exports = class Application extends Chaplin.Application
Chaplin.mediator.user = null
# Add additional application-specific properties and methods
# Seal the mediator
- Chaplin.mediator.seal()
+ # Chaplin.mediator.seal()
+
+ initSocket: (cb) =>
+ socket = io.connect @serverUrl
+ Backbone.socket = socket
+ socket.on 'connect', cb
View
1  app/assets/index.html
@@ -9,6 +9,7 @@
<title>Shack Karma</title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/stylesheets/app.css">
+ <script src="/socket.io/socket.io.js"></script>
<script src="/javascripts/vendor.js"></script>
<script src="/javascripts/app.js"></script>
<script>require('initialize');</script>
View
45 app/controllers/authentication_controller.coffee
@@ -0,0 +1,45 @@
+mediator = require 'mediator'
+Controller = require 'controllers/base/controller'
+User = require 'models/user'
+LoginView = require 'views/login_view'
+
+module.exports = class AuthenticationController extends Controller
+ initialize: ->
+ # Login flow events
+ # @subscribeEvent 'serviceProviderSession', @serviceProviderSession
+
+ # Handle login
+ # @subscribeEvent 'logout', @logout
+ # @subscribeEvent 'userData', @userData
+
+ # Handler events which trigger an action
+
+ # Show the login dialog
+ # @subscribeEvent '!showLogin', @showLoginView
+ # Try to login with a service provider
+ # @subscribeEvent '!login', @triggerLogin
+ # Initiate logout
+ mediator.subscribe '!auth:logout', @logout
+
+ # Determine the logged-in state
+ @getSession()
+
+ # Try to get an existing session from one of the login providers
+ getSession: =>
+ Backbone.socket.emit 'login', null, (user) ->
+ if user?
+ mediator.user = new User user
+ mediator.publish '!auth:success'
+ else
+ mediator.user = new User()
+ @view = new LoginView
+ model: mediator.user
+ mediator.user.on 'change', =>
+ @view.dispose()
+ Backbone.socket.emit 'login', mediator.user.toJSON(), (user) ->
+ mediator.publish '!auth:success'
+
+ logout: =>
+ Backbone.socket.emit 'logout', () ->
+ console.log 'logged out'
+ window.location.reload(true)
View
28 app/controllers/deeds_controller.coffee
@@ -1,6 +1,7 @@
Controller = require 'controllers/base/controller'
-DeedsPageView = require 'views/deeds_views'
+{PageView, AddView, ItemView} = require 'views/deeds_views'
{Deed, Deeds} = require 'models/deeds'
+mediator = require 'mediator'
module.exports = class DeedsController extends Controller
historyURL: 'deeds'
@@ -10,5 +11,26 @@ module.exports = class DeedsController extends Controller
deeds.fetch
success: (data) ->
console.log data
- @view = new DeedsPageView
- collection: deeds
+ @view = new PageView
+ collection: deeds
+
+ add: ->
+ model = new Deed()
+ @view = new AddView
+ model: model
+ @view.on 'added', (rawData) =>
+ # unpack that data
+ data = {}
+ for rawItem in rawData
+ data[rawItem.name] = rawItem.value
+ model.save data,
+ success: =>
+ mediator.publish '!router:route', '/deeds/' + model.id
+
+ item: (params) ->
+ model = new Deed()
+ model.id = decodeURIComponent params.id
+ model.fetch
+ success: =>
+ @view = new ItemView
+ model: model
View
46 app/controllers/session_controller.coffee
@@ -1,46 +0,0 @@
-mediator = require 'mediator'
-Controller = require 'controllers/base/controller'
-User = require 'models/user'
-LoginView = require 'views/login_view'
-
-module.exports = class SessionController extends Controller
- initialize: ->
- # Login flow events
- # @subscribeEvent 'serviceProviderSession', @serviceProviderSession
-
- # Handle login
- # @subscribeEvent 'logout', @logout
- # @subscribeEvent 'userData', @userData
-
- # Handler events which trigger an action
-
- # Show the login dialog
- # @subscribeEvent '!showLogin', @showLoginView
- # Try to login with a service provider
- # @subscribeEvent '!login', @triggerLogin
- # Initiate logout
- # @subscribeEvent '!logout', @triggerLogout
-
- # Determine the logged-in state
- @getSession()
-
- # Instantiate the user with the given data
- createUser: (userData) ->
- mediator.user = new User userData
-
- # Try to get an existing session from one of the login providers
- getSession: =>
- $.ajax
- type: 'POST'
- url: '/authenticate'
- data: '{}'
- dataType: 'json'
- contentType: 'application/json'
- statusCode:
- 401: =>
- @showLoginView()
- success: (data) ->
-
- # Handler for the global !showLoginView event
- showLoginView: =>
- @view = new LoginView()
View
29 app/lib/iosync.coffee
@@ -0,0 +1,29 @@
+Backbone.sync = (method, model, options) ->
+
+ if not model?.url?
+ console.log 'no url!!'
+ return
+
+ url = if _.isFunction(model.url) then model.url() else model.url
+
+ if method is 'read' and model instanceof Backbone.Collection
+ method = 'list'
+ if method is 'read' and model instanceof Backbone.Model
+ url = model.urlRoot
+ options.data = model.id
+
+ if method is 'update'
+ url = model.urlRoot
+
+ url += ':' + method
+ data = options.data or model.toJSON()
+ cb = (err, data) ->
+ if err?
+ options.error err
+ else
+ options.success data
+ if method is 'update'
+ Backbone.socket.emit url, model.id, data, cb
+ else
+ Backbone.socket.emit url, data, cb
+ return null
View
5 app/models/deeds.coffee
@@ -2,8 +2,9 @@ Model = require 'models/base/model'
Collection = require 'models/base/collection'
module.exports.Deed = class Deed extends Model
- urlRoot: '/api/deeds'
+ urlRoot: 'deeds'
+ idAttribute: '_id'
module.exports.Deeds = class Deeds extends Collection
model: Deed
- url: '/api/deeds'
+ url: 'deeds'
View
2  app/routes.coffee
@@ -1,3 +1,5 @@
module.exports = (match) ->
match '', 'home#index'
match 'deeds', 'deeds#index'
+ match 'deeds/add', 'deeds#add'
+ match 'deeds/:id', 'deeds#item'
View
35 app/views/deeds_views.coffee
@@ -1,25 +1,32 @@
+mediator = require 'mediator'
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'
+class DeedListItemView extends View
+ template: require 'views/templates/deed_list_item'
tagName: 'tr'
+ events:
+ 'click': 'click'
+
+ click: (event) =>
+ event.preventDefault()
+ mediator.publish '!router:route', '/deeds/' + @model.id
+
class DeedsCollectionView extends CollectionView
template: require 'views/templates/deedTable'
tagName: 'table'
- className: 'table'
- itemView: DeedItemView
+ className: 'table table-striped table-hover'
+ itemView: DeedListItemView
listSelector: 'tbody'
-
-module.exports = class DeedsPageView extends PageView
+module.exports.PageView = class DeedsPageView extends PageView
template: require 'views/templates/deeds'
className: 'deeds-page'
initialize: (options) =>
+ super
@deedsCollectionView = new DeedsCollectionView
collection: options.collection
@@ -28,3 +35,17 @@ module.exports = class DeedsPageView extends PageView
@deedsCollectionView.container = @$ '#deeds'
@deedsCollectionView.render()
@deedsCollectionView.renderAllItems()
+
+module.exports.AddView = class DeedsAddView extends PageView
+ template: require 'views/templates/deed_add'
+
+ events:
+ 'submit form': 'save'
+
+ save: (event) =>
+ event.preventDefault()
+ @trigger 'added', @$('form').serializeArray()
+ return false
+
+module.exports.ItemView = class DeedItemView extends PageView
+ template: require 'views/templates/deed_item'
View
32 app/views/header_view.coffee
@@ -3,13 +3,25 @@ View = require 'views/base/view'
template = require 'views/templates/header'
module.exports = class HeaderView extends View
- template: template
- id: 'header'
- className: 'navbar navbar-fixed-top'
- container: '#header-container'
- autoRender: true
-
- initialize: ->
- super
- @subscribeEvent 'loginStatus', @render
- @subscribeEvent 'startupController', @render
+ template: template
+ id: 'header'
+ className: 'navbar navbar-fixed-top'
+ container: '#header-container'
+ autoRender: true
+
+ initialize: ->
+ super
+ @subscribeEvent 'loginStatus', @render
+ @subscribeEvent 'startupController', @render
+
+ getTemplateData: =>
+ data = super()
+ data.user = mediator.user.toJSON()
+ return data
+
+ events:
+ 'click a#logout': 'logout'
+
+ logout: (event) ->
+ event.preventDefault()
+ mediator.publish '!auth:logout'
View
10 app/views/login_view.coffee
@@ -4,8 +4,12 @@ PageView = require 'views/base/page_view'
template =
module.exports = class LoginView extends PageView
- template: require 'views/templates/login'
+ template: require 'views/templates/login'
- initialize: ->
- console.log 'login'
+ events:
+ 'submit form': 'submit'
+ submit: (event) =>
+ event.preventDefault()
+ @model.set 'username', @$('#username').val()
+ return false
View
3  app/views/styles/application.styl
@@ -9,3 +9,6 @@
//*
// box-sizing: border-box
+
+#deeds table tbody tr
+ cursor pointer
View
12 app/views/templates/deed_add.jade
@@ -0,0 +1,12 @@
+include mixins/form_helpers
+
+.page-header
+ h1 Create a new Deed
+
+form.form-horizontal
+ fieldset
+ mixin textfield({name:'name', label:'Name'})
+ mixin textarea({name:'description', label:'Description'})
+ .form-actions
+ button.btn.btn-primary Save
+ != ' '
View
3  app/views/templates/deed_item.jade
@@ -0,0 +1,3 @@
+.page-header
+ h1= 'Deed ' + name
+ p= description
View
0  app/views/templates/deedItem.jade → app/views/templates/deed_list_item.jade
File renamed without changes
View
1  app/views/templates/deeds.jade
@@ -1,4 +1,5 @@
.page-header
h1 Deeds
+a.btn#add-deed(href='/deeds/add') Add Deed
#deeds
View
6 app/views/templates/header.jade
@@ -2,14 +2,14 @@
.container
a.brand(href="/") Shackkarma
.btn-group.pull-right
- a.btn.dropdown-toggle(data-toggle="dropdown",href="")
+ a.btn.dropdown-toggle(data-toggle="dropdown",href="#")
i.icon-user
- | Username
+ = user.username
span.caret
ul.dropdown-menu
li: a(href="#") Profile
li.divider
- li: a(href="#") Sign Out
+ li: a#logout(href="#") Logout
.nav-collapse
ul.nav
for item in items
View
9 app/views/templates/login.jade
@@ -1 +1,8 @@
-h1 LOGIN!
+include mixins/form_helpers
+
+legend Login
+form.form-horizontal
+ mixin textfield({name:'username', label:'Benutzername'})
+ .control-group
+ .controls
+ button.btn(type='submit') Anmelden
View
106 app/views/templates/mixins/form_helpers.jade
@@ -1,8 +1,98 @@
-mixin textfield(name, value, label, disabled)
- .control-group
- label.control-label(for=name)= label
- .controls
- if(disabled)
- input.input-xlarge.disabled(type='text', name=name, value=value, disabled)
- else
- input.input-xlarge(type='text', name=name, value=value)
+- function recognitionProbabilityToColor(probability) {
+- if(typeof(probability != 'undefined')) {
+- if(probability >= 99.0) {
+- return 'success';
+- } else if(probability >= 85.0) {
+- return 'warning';
+- } else if(probability >= 0.0) {
+- return 'error';
+- } else {
+- return 'info';
+- }
+- } else {
+- return '';
+- }
+- }
+
+mixin textfield(options)
+ .control-group(class=recognitionProbabilityToColor(options.recognitionProbability))
+ - if(typeof(options.recognitionProbability) != 'undefined') {
+ - if(typeof(options.recognitionProbability.toFixed) != 'undefined') {
+ //
+ = ' ' + options.recognitionProbability.toFixed(2) + ' %'
+ - }
+ - }
+ label.control-label(for=options.name)= options.label
+ .controls
+ - var classArr = [];
+ - if(options.size)
+ - classArr.push(options.size);
+ - else
+ - classArr.push('span8');
+ - if(options.disabled)
+ - classArr.push('disabled');
+ input(class=classArr, type='text', name=options.name, id=options.name, value=options.value, placeholder=options.placeholder || options.label)
+
+mixin textarea(options)
+ .control-group(class=recognitionProbabilityToColor(options.recognitionProbability))
+ - if(typeof(options.recognitionProbability) != 'undefined') {
+ - if(typeof(options.recognitionProbability.toFixed) != 'undefined') {
+ //
+ = ' ' + options.recognitionProbability.toFixed(2) + ' %'
+ - }
+ - }
+ label.control-label(for=options.name)= options.label
+ .controls
+ - var classArr = [];
+ - if(options.size)
+ - classArr.push(options.size);
+ - else
+ - classArr.push('span8');
+ - if(options.disabled)
+ - classArr.push('disabled');
+ textarea(class=classArr, name=options.name, id=options.name, placeholder=options.placeholder || options.label, rows=3)= options.value
+
+mixin select(name, values, label)
+ .clearfix
+ label(for=name)= label
+ .input
+ select.xlarge(name=name)
+ - each value in values
+ - for (key in value) break;
+ option(value=key)= value[key]
+
+mixin checkbox(options)
+ .control-group(class=recognitionProbabilityToColor(options.recognitionProbability))
+ - if(typeof(options.recognitionProbability) != 'undefined') {
+ - if(typeof(options.recognitionProbability.toFixed) != 'undefined') {
+ //
+ = ' ' + options.recognitionProbability.toFixed(2) + ' %'
+ - }
+ - }
+ label.control-label(for=options.name)= options.label
+ .controls
+ .input-append
+ - if(options.value)
+ input(name=options.name, id=options.name, type='checkbox', checked='checked')
+ - else
+ input(name=options.name, id=options.name, type='checkbox')
+
+mixin radiogroup(options)
+ .control-group(class=recognitionProbabilityToColor(options.recognitionProbability))
+ - if(typeof(options.recognitionProbability) != 'undefined') {
+ - if(typeof(options.recognitionProbability.toFixed) != 'undefined') {
+ //
+ = ' ' + options.recognitionProbability.toFixed(2) + ' %'
+ - }
+ - }
+ label.control-label(for=options.name)= options.label
+ .controls
+ .input-append
+ - for value in options.values
+ label.radio
+ - var buttonId = options.name + '_' + value.id
+ - if(value.id == options.value)
+ input(name=options.name, type='radio', id=buttonId, checked='checked')
+ - else
+ input(name=options.name, type='radio', id=buttonId)
+ = value.label
View
5 generators/collection/collection.coffee.hbs
@@ -1,5 +0,0 @@
-Collection = require 'models/base/collection'
-{{#camelize}}{{name}}{{/camelize}} = require 'models/{{name}}'
-
-module.exports = class {{#camelize}}{{pluralName}}{{/camelize}} extends Collection
- model: {{#camelize}}{{name}}{{/camelize}}
View
11 generators/collection/generator.json
@@ -1,11 +0,0 @@
-{
- "files": [
- {
- "from": "collection.coffee.hbs",
- "to": "app/models/{{pluralName}}.coffee"
- }
- ],
- "dependencies": [
- {"name": "collection_test", "params": "{{name}}"}
- ]
-}
View
12 generators/collection_test/collection_test.coffee.hbs
@@ -1,12 +0,0 @@
-Collection = require 'models/base/collection'
-{{#camelize}}{{name}}{{/camelize}} = require 'models/{{name}}'
-{{#camelize}}{{pluralName}}{{/camelize}} = require 'models/{{pluralName}}'
-
-describe '{{#camelize}}{{name}}{{/camelize}}', ->
- beforeEach ->
- @model = new {{#camelize}}{{name}}{{/camelize}}()
- @collection = new {{#camelize}}{{pluralName}}{{/camelize}}()
-
- afterEach ->
- @model.dispose()
- @collection.dispose()
View
9 generators/collection_test/generator.json
@@ -1,9 +0,0 @@
-{
- "files": [
- {
- "from": "collection_test.coffee.hbs",
- "to": "test/models/{{pluralName}}_test.coffee"
- }
- ],
- "dependencies": []
-}
View
5 generators/collection_view/collection_view.coffee.hbs
@@ -1,5 +0,0 @@
-CollectionView = require 'views/base/collection_view'
-{{#camelize}}{{name}}{{/camelize}} = require 'views/{{name}}_view'
-
-module.exports = class {{#camelize}}{{pluralName}}{{/camelize}}View extends CollectionView
- itemView: {{#camelize}}{{name}}{{/camelize}}
View
9 generators/collection_view/generator.json
@@ -1,9 +0,0 @@
-{
- "files": [
- {
- "from": "collection_view.coffee.hbs",
- "to": "app/views/{{name}}.coffee"
- }
- ],
- "dependencies": []
-}
View
3  generators/controller/controller.coffee.hbs
@@ -1,3 +0,0 @@
-Controller = require 'controllers/base/controller'
-
-module.exports = class {{#camelize}}{{pluralName}}{{/camelize}}Controller extends Controller
View
11 generators/controller/generator.json
@@ -1,11 +0,0 @@
-{
- "files": [
- {
- "from": "controller.coffee.hbs",
- "to": "app/controllers/{{name}}_controller.coffee"
- }
- ],
- "dependencies": [
- {"name": "controller_test", "params": "{{name}}"}
- ]
-}
View
5 generators/controller_test/controller_test.coffee.hbs
@@ -1,5 +0,0 @@
-{{#camelize}}{{name}}{{/camelize}} = require 'controllers/{{name}}'
-
-describe '{{#camelize}}{{name}}{{/camelize}}', ->
- beforeEach ->
- @controller = new {{#camelize}}{{name}}{{/camelize}}()
View
9 generators/controller_test/generator.json
@@ -1,9 +0,0 @@
-{
- "files": [
- {
- "from": "controller_test.coffee.hbs",
- "to": "test/controllers/{{name}}_controller_test.coffee"
- }
- ],
- "dependencies": []
-}
View
0  generators/generator/generated_file.coffee.hbs
No changes.
View
13 generators/generator/generator.json
@@ -1,13 +0,0 @@
-{
- "files": [
- {
- "from": "generator.json.hbs",
- "to": "generators/{{name}}/generator.json"
- },
- {
- "from": "generated_file.coffee.hbs",
- "to": "generators/{{name}}/{{name}}.coffee.hbs"
- }
- ],
- "dependencies": []
-}
View
9 generators/generator/generator.json.hbs
@@ -1,9 +0,0 @@
-{
- "files": [
- {
- "from": "{{name}}.coffee.hbs",
- "to": "app/file.coffee"
- }
- ],
- "dependencies": []
-}
View
11 generators/model/generator.json
@@ -1,11 +0,0 @@
-{
- "files": [
- {
- "from": "model.coffee.hbs",
- "to": "app/models/{{name}}.coffee"
- }
- ],
- "dependencies": [
- {"name": "model_test", "params": "{{name}}"}
- ]
-}
View
3  generators/model/model.coffee.hbs
@@ -1,3 +0,0 @@
-Model = require 'models/base/model'
-
-module.exports = class {{#camelize}}{{name}}{{/camelize}} extends Model
View
9 generators/model_test/generator.json
@@ -1,9 +0,0 @@
-{
- "files": [
- {
- "from": "model_test.coffee.hbs",
- "to": "test/models/{{name}}_test.coffee"
- }
- ],
- "dependencies": []
-}
View
5 generators/model_test/model_test.coffee.hbs
@@ -1,5 +0,0 @@
-{{#camelize}}{{name}}{{/camelize}} = require 'models/{{name}}'
-
-describe '{{#camelize}}{{name}}{{/camelize}}', ->
- beforeEach ->
- @model = new {{#camelize}}{{name}}{{/camelize}}()
View
9 generators/page_view/generator.json
@@ -1,9 +0,0 @@
-{
- "files": [
- {
- "from": "page_view.coffee.hbs",
- "to": "app/views/{{name}}_page_view.coffee"
- }
- ],
- "dependencies": []
-}
View
5 generators/page_view/page_view.coffee.hbs
@@ -1,5 +0,0 @@
-PageView = require 'views/base/page_view'
-template = require 'views/templates/{{name}}_page'
-
-module.exports = class {{#camelize}}{{name}}{{/camelize}}PageView extends View
- template: template
View
9 generators/style/generator.json
@@ -1,9 +0,0 @@
-{
- "files": [
- {
- "from": "file.styl.hbs",
- "to": "app/views/styles/{{name}}.styl"
- }
- ],
- "dependencies": []
-}
View
1  generators/style/style.styl.hbs
@@ -1 +0,0 @@
-@import 'nib'
View
9 generators/template/generator.json
@@ -1,9 +0,0 @@
-{
- "files": [
- {
- "from": "template.hbs.hbs",
- "to": "app/views/templates/{{name}}.hbs"
- }
- ],
- "dependencies": []
-}
View
0  generators/template/template.hbs.hbs
No changes.
View
11 generators/view/generator.json
@@ -1,11 +0,0 @@
-{
- "files": [
- {
- "from": "view.coffee.hbs",
- "to": "app/views/{{name}}_view.coffee"
- }
- ],
- "dependencies": [
- {"name": "view_test", "params": "{{name}}"}
- ]
-}
View
5 generators/view/view.coffee.hbs
@@ -1,5 +0,0 @@
-View = require 'views/base/view'
-template = require 'views/templates/{{name}}'
-
-module.exports = class {{#camelize}}{{name}}{{/camelize}}View extends View
- template: template
View
9 generators/view_test/generator.json
@@ -1,9 +0,0 @@
-{
- "files": [
- {
- "from": "view_test.coffee.hbs",
- "to": "test/views/{{name}}_view_test.coffee"
- }
- ],
- "dependencies": []
-}
View
5 generators/view_test/view_test.coffee.hbs
@@ -1,5 +0,0 @@
-{{#camelize}}{{name}}{{/camelize}}View = require 'views/{{name}}_view'
-
-describe '{{#camelize}}{{name}}{{/camelize}}View', ->
- beforeEach ->
- @view = new {{#camelize}}{{name}}{{/camelize}}View()
View
1  lib/routers/Crud.coffee
@@ -7,7 +7,6 @@ module.exports = class Crud
app.put "/api/#{@prefix}/:id", @update
app.delete "/api/#{@prefix}/:id", @delete
-
list: (req, res) =>
@model.find req.query, (err, items) ->
console.log err if err?
View
4 lib/routers/Deed.coffee
@@ -1,6 +1,6 @@
Model = global.mongoose.model 'Deed'
-Crud = require './Crud'
+Crud = require './IoCrud'
module.exports = class ModelRouter extends Crud
model: Model
- prefix: 'deeds'
+ prefix: 'deeds'
View
14 lib/routers/IoCrud.coffee
@@ -9,8 +9,8 @@ module.exports = class IoCrud
@item socket, id, cb
socket.on "#{@prefix}:delete", (_id, cb) =>
@delete socket, _id, cb
- socket.on "#{@prefix}:update", (_id, cb) =>
- @update socket, _id, cb
+ socket.on "#{@prefix}:update", (id, obj, cb) =>
+ @update socket, id, obj, cb
list: (socket, query, cb) =>
@model.find query, (err, items) ->
@@ -41,12 +41,10 @@ module.exports = class IoCrud
return cb err
res.send()
- update: (req, res) =>
- item = req.body
- id = item._id
- delete item._id
- @model.update {_id: id}, item, (err) ->
+ update: (socket, id, obj, cb) =>
+ delete obj._id
+ @model.update {_id: id}, obj, (err) ->
if err?
console.log err
return cb err
- res.send()
+ cb null, obj
View
2  lib/schemas/Deed.coffee
@@ -6,4 +6,4 @@ schema = new Schema
name: String
description: String
-module.exports = schema
+module.exports = mongoose.model 'Deed', schema, 'deeds'
View
12 lib/schemas/User.coffee
@@ -0,0 +1,12 @@
+# user
+mongoose = require 'mongoose'
+Schema = mongoose.Schema
+
+schema = new Schema
+ _id: String
+ displayName: String
+ role:
+ type: [String]
+ default: ['user']
+
+module.exports = mongoose.model 'User', schema, 'users'
View
104 lib/server.coffee
@@ -1,6 +1,9 @@
+# Initialize loggers.
log4js = require 'log4js'
-logger = log4js.getLogger 'shackkarma'
-logger.setLevel 'INFO'
+appLogger = log4js.getLogger 'app'
+accessLogger = log4js.getLogger 'access'
+appLogger.setLevel if process.isTest then 'FATAL' else 'DEBUG'
+accessLogger.setLevel if process.isTest then 'FATAL' else 'DEBUG'
express = require 'express'
path = require 'path'
@@ -13,18 +16,22 @@ mongoose = require 'mongoose'
mongoose.connect 'mongodb://localhost/shackkarma'
global.mongoose = mongoose
-# Project = mongoose.model 'Model', require('./schemas/Model'), 'models'
-
-Deed = mongoose.model 'Deed', require('./schemas/Deed'), 'deeds'
+User = require './schemas/User'
+Deed = require './schemas/Deed'
# Server config
+sessionStore = new express.session.MemoryStore
+ reapInterval: 60000 * 10
app.configure ->
app.use express.bodyParser()
app.use express.methodOverride()
- app.use express.cookieParser 'ponies'
- app.use express.session()
- app.use log4js.connectLogger log4js.getLogger 'shackkarma-access'
+ app.use express.cookieParser()
+ app.use express.session
+ store: sessionStore
+ key: 'sid'
+ secret: 'QWoHD3JsKg1TdhnD'
+ app.use log4js.connectLogger accessLogger
app.use express.static __dirname + '/../public'
app.use app.router
@@ -34,32 +41,11 @@ app.configure 'development', ->
app.configure 'production', ->
app.use express.errorHandler()
-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
-
app.get '*', (req, res) ->
res.sendfile path.normalize __dirname + '/../public/index.html'
server = app.listen 9000, ->
- logger.info "Express server listening on port %d in %s mode", 9000, app.settings.env
+ appLogger.info "Express server listening on port %d in %s mode", 9000, app.settings.env
# io = require('socket.io').listen server
@@ -71,4 +57,60 @@ server = app.listen 9000, ->
# console.log 'on', msg
# socket.on 'message', (msg) ->
# console.log 'msgsock', msg
-# new IoRouter socket
+# new IoRouter socket
+
+
+# Attach Socket.IO to server.
+io = require('socket.io').listen server,
+ logger:
+ debug: ->
+ accessLogger.debug.apply accessLogger, arguments
+ info: ->
+ accessLogger.info.apply accessLogger, arguments
+ warn: ->
+ accessLogger.warn.apply accessLogger, arguments
+ error: ->
+ accessLogger.error.apply accessLogger, arguments
+
+io.set 'authorization', (handshakeData, cb) ->
+ if not handshakeData.headers.cookie
+ handshakeData.session = {}
+ return cb null, true
+ cookieParser = require 'express/node_modules/cookie'
+ connectUtils = require 'express/node_modules/connect/lib/utils'
+ signedCookies = cookieParser.parse handshakeData.headers.cookie
+ handshakeData.cookies = connectUtils.parseSignedCookies signedCookies, 'QWoHD3JsKg1TdhnD'
+
+ sessionId = handshakeData.cookies['sid']
+ sessionStore.get sessionId, (err, session) ->
+ return cb 'no session found', false if err or not session
+ handshakeData.session = session
+ handshakeData.sessionId = sessionId
+ return cb null, true
+
+DeedRouter = require './routers/Deed'
+
+
+io.sockets.on 'connection', (socket) ->
+ # TODO block unauthd access
+ # socket.on 'message', ->
+ # console.log 'message', message
+ # socket.disconnect()
+ socket.on 'login', (user, cb) ->
+ session = socket.handshake.session
+ if not user
+ if session.user
+ cb session.user
+ else
+ cb null
+ return
+ else
+ session.user = user
+ if socket.handshake.sessionId?
+ sessionStore.set socket.handshake.sessionId, session, ->
+ cb user
+ else
+ cb user
+ deedRouter = new DeedRouter socket, session
+ socket.on 'logout', (cb) ->
+ sessionStore.destroy socket.handshake.sessionId, cb
View
3  package.json
@@ -26,7 +26,8 @@
"clean-css-brunch": ">= 1.0 < 1.5",
"express": ">= 3.0",
"log4js": "~0.5.3",
- "mongoose": "~3.2.1"
+ "mongoose": "~3.2.1",
+ "socket.io": "~0.9.11"
},
"devDependencies": {
"chai": "1.2.0",
Please sign in to comment.
Something went wrong with that request. Please try again.