Permalink
Browse files

Move model into module

  • Loading branch information...
1 parent 56a0e96 commit dca13de62d45d92ae23dee24c56fe2d0ea9499fd @edubkendo committed Jun 27, 2012
Showing with 153 additions and 15 deletions.
  1. +18 −0 _server.coffee
  2. +5 −0 app/app.coffee
  3. +107 −4 notes.md
  4. +8 −11 server.coffee
  5. +15 −0 server/model.coffee
  6. 0 server/routes.coffee
View
@@ -0,0 +1,18 @@
+express = require 'express'
+
+exports.startServer = (port, path, callback = (->)) ->
+
+ server = express.createServer()
+
+ server.configure ->
+ server.use express.static path
+ server.set 'views', path
+ server.set 'view options', layout: no
+ server.register '.html', compile: (str, options) ->
+ (locals) -> str
+
+ server.get '/', (req, res) ->
+ res.render 'index.html'
+
+ server.listen parseInt port, 10
+ server.on 'listening', callback
View
@@ -5,4 +5,9 @@ App = Em.Application.create
Models : Em.Namespace.create()
Controllers : Em.Namespace.create()
+App.store = DS.Store.create {
+ revision: 4
+ adapter: DS.RESTAdapter.create { bulkCommit: false }
+}
+
module.exports = App
View
@@ -1,10 +1,12 @@
-Putting together Zappa, Brunch w/ Ember, Mongoose and Sockets.
+# Putting together Zappa, Brunch w/ Ember, Mongoose and Sockets.
+```bash
npm install -g brunch
git clone git://github.com/icholy/ember-brunch.git -b coffee
brunch new myapp -s ./ember-brunch/
+```
(or
npm install -g brunch
@@ -13,14 +15,115 @@ npm install .
brunch build
)
-set up server.coffee to start our server with zappajs, and serve the public folder. We also go ahead and set up 'mongoose' to be our MongoDB adapter:
+### Set up server
+
+In server.coffee, start our server with zappajs, and serve the public folder with "@use 'static'". We also log a simple message when a client makes a socket.io connection.
```coffee
require('zappajs') ->
+ require('./server/model')(@app)
+
+ @use 'static'
+
+ @on connection: ->
+ console.log 'Hello there! App is now connected.'
+```
+### The server-side model
+
+Set up 'mongoose' to be our MongoDB adapter, define a Schema and mongoose.model in /server/model.coffee :
+
+```coffee
+
+model = (@app) ->
+
+ @app.mongoose = require 'mongoose'
+ @app.db = @app.mongoose.connect('mongodb://localhost/zapp_ember_database')
+
+ @app.Schema = @app.mongoose.Schema
+
+ @app.ContactSchema = new @app.Schema
+ firstName: String
+ lastName: String
+
+ @app.Contact = @app.mongoose.model('Contact', @app.ContactSchema)
+
+module.exports = model
+```
+### Function to easily delete a model from the database.
+
+Removing a user from the database proved to be a little tricky, so this is a helper function in server.coffee
+
+```coffee
+
+ modelDel = (@q) ->
+ @app.Contact.find(@q, (err, docs) ->
+ doc.remove() for doc in docs
+ )
+```
+
+
+
+
+## Extra Notes
+
+Note: To establish a socket.io "connection", on the client side you need some code like:
+
+```html
+ <script src="/socket.io/socket.io.js"></script>
+ <script>
+ var socket = this.io.connect('http://localhost');
+ if (socket != undefined)
+ console.log("Connection established");
+ </script>
+```
+ # An example of finding and logging users:
+ #
+ # userB = ->
+ # @app.Contact.findOne({ firstName: user.firstName }, (err, doc) ->
+ # if err
+ # console.log err
+ # else
+ # console.log doc
+ # )
+
+ # userB()
+
+ # An example of finding and deleting users from the database:
+ #
+ # @app.Contact.find({ firstName: 'Bob' }, (err, docs) ->
+ # if err
+ # console.log err
+ # else
+ # for doc in docs
+ # doc.remove()
+ # userB()
+ # )
+
+### All in server.coffee
+
+If you leave everything in server.coffee, instead of using a module, you can do it like:
+
+```coffee
mongoose = require 'mongoose'
db = mongoose.connect('mongodb://localhost/zapp_ember_database')
- @use 'static'
-```
+ Schema = mongoose.Schema
+
+ ContactSchema = new Schema
+ firstName: String
+ lastName: String
+
+ Contact = mongoose.model('Contact', ContactSchema)
+
+ user = new Contact
+ user.firstName = "Bob"
+ user.lastName = "Bobkinnick"
+ user.save()
+
+ console.log user
+ Contact.findOne({ _id: user._id }, (err, doc) ->
+ doc.remove()
+ )
+ ```
View
@@ -1,15 +1,12 @@
require('zappajs') ->
+ require('./server/model')(@app)
- mongoose = require 'mongoose'
- db = mongoose.connect('mongodb://localhost/zapp_ember_database')
+ @use 'static'
- Schema = mongoose.Schema
+ modelDel = (@q) ->
+ @app.Contact.find(@q, (err, docs) ->
+ doc.remove() for doc in docs
+ )
- ContactSchema = new Schema
- firstName: String
- lastName: String
-
- Contact = mongoose.model('Contact', ContactSchema)
-
-
- @use 'static'
+ @on connection: ->
+ console.log 'Hello there! App is now connected.'
View
@@ -0,0 +1,15 @@
+
+model = (@app) ->
+
+ @app.mongoose = require 'mongoose'
+ @app.db = @app.mongoose.connect('mongodb://localhost/zapp_ember_database')
+
+ @app.Schema = @app.mongoose.Schema
+
+ @app.ContactSchema = new @app.Schema
+ firstName: String
+ lastName: String
+
+ @app.Contact = @app.mongoose.model('Contact', @app.ContactSchema)
+
+module.exports = model
No changes.

0 comments on commit dca13de

Please sign in to comment.