trailsjs / trails

:evergreen_tree: Modern Web Application Framework for Node.js.
http://trailsjs.io
Other
1.66k stars 70 forks source link

HMVC #28

Closed AlMcKinlay closed 8 years ago

AlMcKinlay commented 8 years ago

I was wondering if you guys would be open to supporting an HMVC setup with trails? It's something that I've found has improved my development massively, and currently I'm doing it with my front-end code, but as I'm using sails, I can't use it with my backend code. It would be great if we could get this.

For those unaware, currently the backend is defined like this:

HMVC is where you could organise it more like this:

And it means that everything you need for a particular feature is in the folder together.

Ideally, we would also be able to add the routes as individual files into these folders as well, that would be brilliant. I really hope you look to doing this, as it would mean that there was a massive reason for me to use Trails (as I've not found a good node framework that uses hmvc yet).

tjwebb commented 8 years ago

HMVC is where you could organise it more like this:

That sounds like a fine idea; because Trails doesn't dynamically require() things, you can structure the namespace however you want, as long as it ends up in the Trails format below:

app = {
  api: {
    controllers: { ... },
    models: { ... },
    services: { ... }
  },
  config: { ... }
}

In practice, we want to encourage a particular convention. The current design is how Rails and Grails organize their directories, so users coming from those frameworks (or others that are inspired by them, like Sails) are familiar already with where things are.

I see two ways to do this.

1. you can manually setup the require's to load stuff like this. e.g.

// api/index.js
exports.controllers = {
  UserController: require('./User/UserController'),
  CommentController: require('./Comment/CommentController'),
  // etc...
}
exports.models = {
  User: require('./User/UserModel')
  // etc ...
}

You'd probably want to create a modified generator to do this automatically.

2. you could write your own trailpack to translate the object all at once.

AlMcKinlay commented 8 years ago

Brilliant, I hadn't even noticed the index.js files, that makes things much easier.

Cool, I'm going to have a play around, make a fork of the example, and get that working, and then I'll probably look at getting some yo scripts for it as well. Would there be any interest in these for anyone in the future wanting to do this?

tjwebb commented 8 years ago

@YaManicKill awesome, let me know what you come up with. Yea, I'd be open to merging in a trails:hmvc subgenerator that sets up the hmvc directory structure. Then also you'll want to update the logic that inserts the require statements into the index files when new models/controllers/etc are generated.

tjwebb commented 8 years ago

I think this work can fall entirely under the purview of generator-trails, so @YaManicKill open an issue over there and we can continue discussion there. sound good?

AlMcKinlay commented 8 years ago

Alrighty, created trailsjs/generator-trails#12. Closing this.