This commit is contained in:
Richard Pringle 2017-05-24 14:57:38 +00:00 committed by GitHub
commit 47c9bf9ceb
1 changed files with 67 additions and 1 deletions

View File

@ -19,6 +19,7 @@ var classify = require('underscore.string/classify');
var camelize = require('underscore.string/camelize');
var path = require('path');
var util = require('util');
var EventEmitter = require('events');
/**
* The `App` object represents a Loopback application.
@ -291,7 +292,10 @@ app.handler = function(type, options) {
var handler = this._handlers[type] = remotes.handler(type, options);
remotes.classes().forEach(function(sharedClass) {
sharedClass.ctor.emit('mounted', app, sharedClass, remotes);
/* HACK!!! */
if (sharedClass.name !== 'ctrl') {
sharedClass.ctor.emit('mounted', app, sharedClass, remotes);
}
});
return handler;
@ -659,3 +663,65 @@ app.listen = function(cb) {
return server;
};
/* START CONTROLLER CODE*/
// this is similar to app.model
// at the moment I haven't really used 'options'
app.controller = function(ctor, options) {
var self = this;
// the SharedController is a wrapper, see below
var controller = new SharedController(ctor);
this.remotes().addClass(controller.sharedClass);
app.controllers().push(controller);
return controller;
// SharedController class contains strong-remoting objects to set up the metadata
// and register the endpoint, etc
function SharedController(ctor, options) {
options = options || {};
this.name = ctor.name;
this.ctor = ctor;
// TODO - right now there's a hack on line 289 to skip this
// util.inherits(ctor, EventEmitter);
this.sharedClass = new RemoteObjects.SharedClass(this.name, ctor, options);
// the resolveScope method instantiates the controller upon request
// (please correct my terminology if it's wrong)
this.sharedClass.resolveScope = function(remotingContext) {
// self is the app - should it be something more obvious like appInstance?
return new ctor(self, remotingContext);
};
// similar to model.remoteMethod
this.remoteMethod = function(name, options) {
// I didn't dive into why, but this only worked when I set isStatic to true
// whether the controller methods were prototype methods or not
options.isStatic = true;
this.sharedClass.defineMethod(name, options);
};
// have not tested this method yet but it follows model.disableRemoteMethod
this.disableRemoteMethod = function(name, isStatic) {
this.sharedClass.disableMethod(name, isStatic || false);
// this.emit('remoteMethodDisabled', this.sharedClass, name);
};
};
};
// similar to this app.models
// the controllers still need to registered in the Registy though
app.controllers = function() {
return this._controllers || (this._controllers = []);
};
// TODO
// need to add the EventEmitter to the constructor of the controller
// function ctrl() {
// // EventEmitter.call(this);
// };
// util.inherits(ctrl, EventEmitter);
/* END CONTROLLER CODE */