Implement working controller PoC.

This commit is contained in:
Richard Pringle 2016-04-29 12:58:28 -04:00
parent b46b336775
commit 7272f5e17e
1 changed files with 41 additions and 32 deletions

View File

@ -285,6 +285,7 @@ app.handler = function(type, options) {
var handler = this._handlers[type] = remotes.handler(type, options);
remotes.classes().forEach(function(sharedClass) {
/* HACK!!! */
if (sharedClass.name !== 'ctrl') {
sharedClass.ctor.emit('mounted', app, sharedClass, remotes);
}
@ -573,47 +574,55 @@ app.listen = function(cb) {
/* START CONTROLLER CODE*/
// Implementation without wrapper
// app.controller = function(controllerCtor, options) {
// var self = this;
// SharedClass = RemoteObjects.SharedClass;
// controllerCtor._sharedClass = new SharedClass(controllerCtor.name, controllerCtor);
// controllerCtor._sharedClass.resolve = function() {
// return new controllerCtor(app, remotingContext);
// };
// app.remotes().addClass(controllerCtor._sharedClass);
// };
// Implementation with wrapper
// 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;
};
app.controllers = function() {
return this._controllers || (this._controllers = []);
};
function SharedController(ctor, options) {
// 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;
util.inherits(ctor, EventEmitter);
this.sharedClass = new RemoteObjects.SharedClass(this.name, ctor, options);
};
// TODO - right now there's a hack on line 289 to skip this
// util.inherits(ctor, EventEmitter);
SharedController.prototype.remoteMethod = function(name, options) {
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);
};
};
};
SharedController.prototype.disableRemoteMethod = function(name, isStatic) {
this.sharedClass.disableMethod(name, isStatic || false);
// similar to this app.models
// the controllers still need to registered in the Registy though
app.controllers = function() {
return this._controllers || (this._controllers = []);
};
// TODO