strongloop/loopback — pull request #532 by ritch (2014-09-04 01:43:16+00:00)
Title: Add app.ready() and Model ready hooks

@bajtos @raymondfeng @fabien @kraman

This adds a set of ready hooks to `Model` classes. Override these hooks to implement custom behavior that depends on your app being bootstrapped:

- models attached to datasources, methods mixed in
- relations, remoting, and other metadata defined
- boot scripts run, `loopback-boot` complete
- etc.

## Docs

**Do not call this method** if you are using `loopback-boot` to bootstrap your application.
`loopback-boot` will call this method for you! Otherwise you must call `app.ready()` to run
the `Model.ready()` hooks.

Calling `ready()` will call `ready()` on all models attached to the `app`. Override
the `ready()` method on a `Model` class to ensure you have access to a bootstrapped application.

```js
module.exports = function(MyModel) {
MyModel.setup = function() {
// setup is called when a model is extended
// you must call `base.setup()` to extend the base class properly
this.base.setup();

// add or remove remote methods and otherwise modify the `Model`
this.remoteMethod('myMethod');
}

MyModel.beforeReady = function(cb) {
// async setup, runs after all models are `setup()`
// and before `ready()` is called
setTimeout(cb, 100);
}

MyModel.ready = function() {
// MyModel and other classes can be used
// you should not modify any classes in this method
console.log(this.sharedClass.methods());
}
}
```