diff --git a/lib/model.js b/lib/model.js index 804886bf..1046af70 100644 --- a/lib/model.js +++ b/lib/model.js @@ -388,9 +388,11 @@ module.exports = function(registry) { */ Model.getApp = function(callback) { - this._runWhenAttachedToApp(function(app) { - assert(Model.app); - callback(null, Model.app); + var self = this; + self._runWhenAttachedToApp(function(app) { + assert(self.app); + assert.equal(app, self.app); + callback(null, app); }); }; diff --git a/test/model.test.js b/test/model.test.js index abe4f1d5..3ce499ab 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -618,4 +618,33 @@ describe.onServer('Remote Methods', function() { ]); }); }); + + describe('Model.getApp(cb)', function() { + var app, TestModel; + beforeEach(function setup() { + app = loopback(); + TestModel = loopback.createModel('TestModelForGetApp'); // unique name + app.dataSource('db', { connector: 'memory' }); + }); + + it('calls the callback when already attached', function(done) { + app.model(TestModel, { dataSource: 'db' }); + TestModel.getApp(function(err, a) { + if (err) return done(err); + expect(a).to.equal(app); + done(); + }); + // fails on time-out when not implemented correctly + }); + + it('calls the callback after attached', function(done) { + TestModel.getApp(function(err, a) { + if (err) return done(err); + expect(a).to.equal(app); + done(); + }); + app.model(TestModel, { dataSource: 'db' }); + // fails on time-out when not implemented correctly + }); + }); });