Emit a 'modelRemoted' event by app.model()

This event will be listened by loopback-explorer so that models remoted
after the explorer is initiated will be added to the api specs
This commit is contained in:
Raymond Feng 2014-07-24 17:00:27 -07:00
parent 405d04f4ad
commit af26e09845
3 changed files with 20 additions and 1 deletions

View File

@ -152,9 +152,11 @@ app.model = function (Model, config) {
if (isPublic && Model.sharedClass) { if (isPublic && Model.sharedClass) {
this.remotes().addClass(Model.sharedClass); this.remotes().addClass(Model.sharedClass);
if (Model.settings.trackChanges && Model.Change) if (Model.settings.trackChanges && Model.Change) {
this.remotes().addClass(Model.Change.sharedClass); this.remotes().addClass(Model.Change.sharedClass);
}
clearHandlerCache(this); clearHandlerCache(this);
this.emit('modelRemoted', Model.sharedClass);
} }
Model.shared = isPublic; Model.shared = isPublic;

View File

@ -1,3 +1,6 @@
var EventEmitter = require('events').EventEmitter;
var util = require('util');
module.exports = browserExpress; module.exports = browserExpress;
function browserExpress() { function browserExpress() {
@ -10,6 +13,8 @@ function BrowserExpress() {
this.settings = {}; this.settings = {};
} }
util.inherits(BrowserExpress, EventEmitter);
BrowserExpress.prototype.set = function(key, value) { BrowserExpress.prototype.set = function(key, value) {
if (arguments.length == 1) { if (arguments.length == 1) {
return this.get(key); return this.get(key);

View File

@ -47,6 +47,18 @@ describe('app', function() {
expect(app.models.Color).to.equal(Color); expect(app.models.Color).to.equal(Color);
}); });
it("emits a `modelRemoted` event", function() {
var Color = PersistedModel.extend('color', {name: String});
Color.shared = true;
var remotedClass;
app.on('modelRemoted', function(sharedClass) {
remotedClass = sharedClass;
});
app.model(Color);
expect(remotedClass).to.exist;
expect(remotedClass).to.eql(Color.sharedClass);
});
it.onServer('updates REST API when a new model is added', function(done) { it.onServer('updates REST API when a new model is added', function(done) {
app.use(loopback.rest()); app.use(loopback.rest());
request(app).get('/colors').expect(404, function(err, res) { request(app).get('/colors').expect(404, function(err, res) {