Implement working controller PoC.
This commit is contained in:
parent
b46b336775
commit
7272f5e17e
|
@ -285,6 +285,7 @@ app.handler = function(type, options) {
|
||||||
var handler = this._handlers[type] = remotes.handler(type, options);
|
var handler = this._handlers[type] = remotes.handler(type, options);
|
||||||
|
|
||||||
remotes.classes().forEach(function(sharedClass) {
|
remotes.classes().forEach(function(sharedClass) {
|
||||||
|
/* HACK!!! */
|
||||||
if (sharedClass.name !== 'ctrl') {
|
if (sharedClass.name !== 'ctrl') {
|
||||||
sharedClass.ctor.emit('mounted', app, sharedClass, remotes);
|
sharedClass.ctor.emit('mounted', app, sharedClass, remotes);
|
||||||
}
|
}
|
||||||
|
@ -573,49 +574,57 @@ app.listen = function(cb) {
|
||||||
|
|
||||||
/* START CONTROLLER CODE*/
|
/* START CONTROLLER CODE*/
|
||||||
|
|
||||||
// Implementation without wrapper
|
// this is similar to app.model
|
||||||
|
// at the moment I haven't really used 'options'
|
||||||
// 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
|
|
||||||
|
|
||||||
app.controller = function(ctor, options) {
|
app.controller = function(ctor, options) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
// the SharedController is a wrapper, see below
|
||||||
var controller = new SharedController(ctor);
|
var controller = new SharedController(ctor);
|
||||||
this.remotes().addClass(controller.sharedClass);
|
this.remotes().addClass(controller.sharedClass);
|
||||||
app.controllers().push(controller);
|
app.controllers().push(controller);
|
||||||
|
|
||||||
return 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() {
|
app.controllers = function() {
|
||||||
return this._controllers || (this._controllers = []);
|
return this._controllers || (this._controllers = []);
|
||||||
};
|
};
|
||||||
|
|
||||||
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);
|
|
||||||
};
|
|
||||||
|
|
||||||
SharedController.prototype.remoteMethod = function(name, options) {
|
|
||||||
options.isStatic = true;
|
|
||||||
this.sharedClass.defineMethod(name, options);
|
|
||||||
};
|
|
||||||
|
|
||||||
SharedController.prototype.disableRemoteMethod = function(name, isStatic) {
|
|
||||||
this.sharedClass.disableMethod(name, isStatic || false);
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
// need to add the EventEmitter to the constructor of the controller
|
// need to add the EventEmitter to the constructor of the controller
|
||||||
// function ctrl() {
|
// function ctrl() {
|
||||||
|
|
Loading…
Reference in New Issue