最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.JS - Using prototype in a module - Stack Overflow

programmeradmin3浏览0评论

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 mon file.

You can see my mon controller file here:

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:

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!

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 mon file.

You can see my mon controller file here: https://gist.github./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./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!

Share Improve this question asked Feb 20, 2012 at 16:31 John HamelinkJohn Hamelink 1,0882 gold badges15 silver badges33 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 14

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./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 bee 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./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./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./2011/11/11/js-object-inheritance/
  • http://yehudakatz./2011/08/12/understanding-prototypes-in-javascript/

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

  • http://www.codinghorror./blog/2006/07/i-pity-the-fool-who-doesnt-write-unit-tests.html
发布评论

评论列表(0)

  1. 暂无评论