Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.JS - Using prototype in a module

So I'm writing a whole bunch of vendor-specific files in node which all have a similar controller pattern, so it makes sense for me to cut them out and put into a common file.

You can see my common controller file here: https://gist.github.com/081a04073656bf28f46b

Now when I use them in my multiple modules, each consecutively loaded module is overwriting the first. This is because the file is only required once and passed dynamically through to each module on load (this allows me to add extra modules and these modules are able to add their own routes, for example). You can see an example module here: https://gist.github.com/2382bf93298e0fc58599

You can see here on line 53 I've realised that we need to create a seperate instance every time, so I've tried to create a new instance by copying the standardControllers object into a new object, then initialising the new object. This has zero impact on the code, and the code behaves in exactly the same way.

Any ideas guys? I'm in a bit of a jam with this one!

like image 744
John Hamelink Avatar asked Dec 10 '22 02:12

John Hamelink


1 Answers

First thing I'd do is try to make things simpler and reduce coupling by invoking the single responsibility principle, et al. http://www.codinghorror.com/blog/2007/03/curlys-law-do-one-thing.html

Put those Schemas into their own files, eg

models/client.js
models/assistant.js
models/contact.js

I've also found that embedded docs + mongoose is generally a PITA. I'd probably promote all those to top level docs.

You don't need to enclose your object's keys in quotes.

routes = {
   list: function() {} // no quotes is aok
}

Also 'list' in typical REST apps is called 'index'. Anyway.

Ok, I'd break this up differently. Since you're requiring stuff from the index.js file in the middleware, they become tightly coupled, which is bad. in fact, I think I'd rewrite this whole thing so it was tidier. Sorry.

I'd probably replace your 'middleware' file with an express-resource controller https://github.com/visionmedia/express-resource (built by author of express). This is a good framework for restful controllers, such as what you're building. The auto-loader is really sweet.

You may also want to look at: http://mcavage.github.com/node-restify/ It's new, I haven't tried it out, but I've heard good things.

Since what you're building is basically an automated mongoose-crud system, with optional overriding, I'd create an express-resource controller as your base

/controllers/base_controller.js

and it might look like

var BaseController = function() {} // BaseController constructor

BaseController.prototype.index = function() {
   // copy from your middleware
}
BaseController.prototype.show = function() {
   // copy from your middleware
}
BaseController.prototype.create = function() {
   // copy from your middleware
}
// etc

module.exports = BaseController

Then I'd do something like:

/controllers/some_resource_controller.js

which might look something like:

var BaseController = require('./base_controller')
var NewResourceController = function() {
    // Apply BaseController constructor (i.e. call super())
    BaseController.apply(this, arguments) 
}

NewResourceController.prototype = new Base()

NewResourceController.prototype.create = function() {
    // custom create method goes here
}



module.exports = NewResourceController

Then to use it, you can do:

var user = app.resource(myResourceName, new ResourceController());

…inside some loop which sets myResourceName to be whatever crud you're trying to set up.

Here's some links for you to read:

  • http://tobyho.com/2011/11/11/js-object-inheritance/
  • http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/

Also, it sounds like you're not writing tests. Write tests.

  • http://www.codinghorror.com/blog/2006/07/i-pity-the-fool-who-doesnt-write-unit-tests.html
like image 85
timoxley Avatar answered Dec 11 '22 16:12

timoxley