Merge pull request #402 from strongloop/feature/emit-model-remoted-event

Emit a 'modelRemoted' event by app.model()
This commit is contained in:
Raymond Feng 2014-07-25 09:06:46 -07:00
commit 5aee3b7f61
3 changed files with 20 additions and 1 deletions

View File

@ -152,9 +152,11 @@ app.model = function (Model, config) {
if (isPublic && 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);
}
clearHandlerCache(this);
this.emit('modelRemoted', Model.sharedClass);
}
Model.shared = isPublic;

View File

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

View File

@ -47,6 +47,18 @@ describe('app', function() {
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) {
app.use(loopback.rest());
request(app).get('/colors').expect(404, function(err, res) {