Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

update readme #19

Merged
merged 1 commit into from

2 participants

Aaron Heckmann Nik Martin
Aaron Heckmann
Owner

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
Commits on Dec 6, 2012
  1. Aaron Heckmann

    update readme

    aheckmann authored
    explain that all thats necessary is the modules inclusion to perform the bindings.
    add js highlighting.
This page is out of date. Refresh to see the latest.
Showing with 63 additions and 47 deletions.
  1. +63 −47 Readme.md
110 Readme.md
View
@@ -17,67 +17,83 @@ Methods which now support `Queries`:
$ npm install express-mongoose
+### Use
+
+Include `express-mongoose` in your project, which performs the necessary bindings between `express` and `mongoose`.
+
+```js
+require('express-mongoose') // thats it!
+```
+
### Example
In your schemas:
- UserSchema.methods.getLikes = function (callback) {
- // returns a Query
- return this.model('Likes').find({ _user: this._id }, callback);
- };
-
- NewsSchema.statics.getLatest = function (callback) {
- var promise = new Promise;
- if (callback) promise.addBack(callback);
- this.find({ datePublished: { $gt: new Date(Date.now() - 60000*60) } }, promise.resolve.bind(promise));
- return promise;
- };
+```js
+UserSchema.methods.getLikes = function (callback) {
+ // returns a Query
+ return this.model('Likes').find({ _user: this._id }, callback);
+};
+
+NewsSchema.statics.getLatest = function (callback) {
+ var promise = new Promise;
+ if (callback) promise.addBack(callback);
+ this.find({ datePublished: { $gt: new Date(Date.now() - 60000*60) } }, promise.resolve.bind(promise));
+ return promise;
+};
+```
In your routes:
- app.get('/dashboard', function (req, res) {
- var News = db.model('News');
-
- // render support
- res.render('dashboard', {
- likes: req.user.getLikes()
- , latestNews: News.getLatest()
- , stuff: new Promise(somethingAsync)
- });
- });
+```js
+app.get('/dashboard', function (req, res) {
+ var News = db.model('News');
+
+ // render support
+ res.render('dashboard', {
+ likes: req.user.getLikes()
+ , latestNews: News.getLatest()
+ , stuff: new Promise(somethingAsync)
+ });
+});
+```
With `res.send` support you can pass a `Query` or `Promise` and the result will be rendered as json.
If an error occurs, the error will be passed to `next()` as expected.
- app.get('/send', function (req, res) {
- var News = db.model('News');
- res.send(News.getLatest());
- });
-
- app.get('/promises', function (req, res) {
- var promise = new Promise(somethingAsync);
- res.send(promise);
- });
-
- app.get('/more', function (req, res) {
- res.send({
- promise: new Promise(somethingAsync)
- , news: req.user.getLatest()
- });
- });
+```js
+app.get('/send', function (req, res) {
+ var News = db.model('News');
+ res.send(News.getLatest());
+});
+
+app.get('/promises', function (req, res) {
+ var promise = new Promise(somethingAsync);
+ res.send(promise);
+});
+
+app.get('/more', function (req, res) {
+ res.send({
+ promise: new Promise(somethingAsync)
+ , news: req.user.getLatest()
+ });
+});
+```
`res.redirect` accepts a `Promise` as well.
- app.get('/redirect', function (req, res) {
- var promise = new Promise;
- res.redirect(promise);
-
- process.nextTick(function () {
- promise.complete(url [, status]);
- // or
- promise.error(new Error('uh oh'));
- });
- });
+```js
+app.get('/redirect', function (req, res) {
+ var promise = new Promise;
+ res.redirect(promise);
+
+ process.nextTick(function () {
+ promise.complete(url [, status]);
+ // or
+ promise.error(new Error('uh oh'));
+ });
+});
+```
### Error handling
Something went wrong with that request. Please try again.