Comments (4)
Sounds good to me. Based on your comments in #64, might need to be flexible with the file names though.
foo_controller.coffee
foo-controller.coffee
FooController.coffee
each is the foo
controller
Basically take the filename, camelCase it, and remove Controller
.
from exprestive.
When I was first designing exprestive I debated whether to use classes or just export an object. I chose classes for a few reasons, and still think that was the right decision.
Here are a few reasons I think classes make sense
- Dependency injection - In every project I've used exprestive, I've found the dependencies feature incredibly useful. Basically you can specify dependencies from the server that are available in the constructor of every controller. This is useful if your app has services that are initialized in the server but need to be accessed by your controllers.
- Loss of name attribute - Named functions in javascript (like coffeescript/livescript classes) automatically get a
name
attribute which we use to reference from the routes file. Using file names to resolve this, as you suggest seems brittle, and introduces a lot more edge cases. This name is also used automatically in error reporting when a controller method raises an error. Finally, it permanently prevents node apps using exprestive from being compiled down to a single file, while this is not something we do, it is a practice I've seen app developers do and our architecture should be flexible enough to not care how you structure your app. - Extend a base controller - Extending common functionality in controllers comes free in coffeescript/livescript. This is useful functionality if you have a lot of CRUD routes. Using a base class you could do some magic to automatically wire up the RESTful verbs (index, show, etc...). Exprestive also provides an optional base controller that offers some helper methods.
- Classes have less magic - We could implement a lot of the issues I've documented here by using methods, but it would require significant more magic. Javascript developers are familiar with classes, they know how to create private helper methods, extend base classes, and how to call these methods from their controller methods. If I didn't know how to use exprestive and I came across an app that was using it and I saw your syntax, I would not intuitively know if I could just create private helper methods and reference them with
this
from my controller methods. - What are we actually saving by changing this - In coffeescript/livescript you're saving 4 characters:
class
. For simple apps an object of methods is a good fit, but using a class isn't any more difficult, but provides much more flexibility. I'm ok with breaking changes, but only if there is a real advantage for doing so. I'd rather not have major breaking changes for the sake of having breaking changes.
from exprestive.
Forgot about dependencies and extending the base controller. Those are far easier with classes then introducing some new syntax.
from exprestive.
Okay, sounds good. Thanks!
from exprestive.
Related Issues (12)
- Use verbatim routes files HOT 4
- Make reverse route helpers return a non-primitive string with method attribute HOT 12
- Better errors
- Support for _method HOT 1
- res.locals.routes is not set in controller actions HOT 1
- Support for scopes HOT 1
- Access reverse routes from this.routes from controllers HOT 5
- Set @routes in middleware functions HOT 2
- Use simple *.js files. HOT 2
- adding middleware on a per-scope basis HOT 2
- support es6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from exprestive.