Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

update readme #19

Merged
merged 1 commit into from 11 months ago

2 participants

Aaron Heckmann Nik Martin
Aaron Heckmann
Collaborator

explain that all thats necessary is the modules inclusion to perform the bindings.

add js highlighting.

fixes #15

Aaron Heckmann aheckmann update readme
explain that all thats necessary is the modules inclusion to perform the bindings.
add js highlighting.
be78456
Nik Martin

BUMP LearnBoost

Aaron Heckmann aheckmann merged commit febefcb into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 06, 2012
Aaron Heckmann aheckmann update readme
explain that all thats necessary is the modules inclusion to perform the bindings.
add js highlighting.
be78456
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 63 additions and 47 deletions. Show diff stats Hide diff stats

  1. +63 47 Readme.md
110 Readme.md
Source Rendered
@@ -17,67 +17,83 @@ Methods which now support `Queries`:
17 17
18 18 $ npm install express-mongoose
19 19
  20 +### Use
  21 +
  22 +Include `express-mongoose` in your project, which performs the necessary bindings between `express` and `mongoose`.
  23 +
  24 +```js
  25 +require('express-mongoose') // thats it!
  26 +```
  27 +
20 28 ### Example
21 29
22 30 In your schemas:
23 31
24   - UserSchema.methods.getLikes = function (callback) {
25   - // returns a Query
26   - return this.model('Likes').find({ _user: this._id }, callback);
27   - };
28   -
29   - NewsSchema.statics.getLatest = function (callback) {
30   - var promise = new Promise;
31   - if (callback) promise.addBack(callback);
32   - this.find({ datePublished: { $gt: new Date(Date.now() - 60000*60) } }, promise.resolve.bind(promise));
33   - return promise;
34   - };
  32 +```js
  33 +UserSchema.methods.getLikes = function (callback) {
  34 + // returns a Query
  35 + return this.model('Likes').find({ _user: this._id }, callback);
  36 +};
  37 +
  38 +NewsSchema.statics.getLatest = function (callback) {
  39 + var promise = new Promise;
  40 + if (callback) promise.addBack(callback);
  41 + this.find({ datePublished: { $gt: new Date(Date.now() - 60000*60) } }, promise.resolve.bind(promise));
  42 + return promise;
  43 +};
  44 +```
35 45
36 46 In your routes:
37 47
38   - app.get('/dashboard', function (req, res) {
39   - var News = db.model('News');
40   -
41   - // render support
42   - res.render('dashboard', {
43   - likes: req.user.getLikes()
44   - , latestNews: News.getLatest()
45   - , stuff: new Promise(somethingAsync)
46   - });
47   - });
  48 +```js
  49 +app.get('/dashboard', function (req, res) {
  50 + var News = db.model('News');
  51 +
  52 + // render support
  53 + res.render('dashboard', {
  54 + likes: req.user.getLikes()
  55 + , latestNews: News.getLatest()
  56 + , stuff: new Promise(somethingAsync)
  57 + });
  58 +});
  59 +```
48 60
49 61 With `res.send` support you can pass a `Query` or `Promise` and the result will be rendered as json.
50 62 If an error occurs, the error will be passed to `next()` as expected.
51 63
52   - app.get('/send', function (req, res) {
53   - var News = db.model('News');
54   - res.send(News.getLatest());
55   - });
56   -
57   - app.get('/promises', function (req, res) {
58   - var promise = new Promise(somethingAsync);
59   - res.send(promise);
60   - });
61   -
62   - app.get('/more', function (req, res) {
63   - res.send({
64   - promise: new Promise(somethingAsync)
65   - , news: req.user.getLatest()
66   - });
67   - });
  64 +```js
  65 +app.get('/send', function (req, res) {
  66 + var News = db.model('News');
  67 + res.send(News.getLatest());
  68 +});
  69 +
  70 +app.get('/promises', function (req, res) {
  71 + var promise = new Promise(somethingAsync);
  72 + res.send(promise);
  73 +});
  74 +
  75 +app.get('/more', function (req, res) {
  76 + res.send({
  77 + promise: new Promise(somethingAsync)
  78 + , news: req.user.getLatest()
  79 + });
  80 +});
  81 +```
68 82
69 83 `res.redirect` accepts a `Promise` as well.
70 84
71   - app.get('/redirect', function (req, res) {
72   - var promise = new Promise;
73   - res.redirect(promise);
74   -
75   - process.nextTick(function () {
76   - promise.complete(url [, status]);
77   - // or
78   - promise.error(new Error('uh oh'));
79   - });
80   - });
  85 +```js
  86 +app.get('/redirect', function (req, res) {
  87 + var promise = new Promise;
  88 + res.redirect(promise);
  89 +
  90 + process.nextTick(function () {
  91 + promise.complete(url [, status]);
  92 + // or
  93 + promise.error(new Error('uh oh'));
  94 + });
  95 +});
  96 +```
81 97
82 98 ### Error handling
83 99

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.