I was thinking … when I code a web app using Backbone.js, most of the time I duplicate the models : one version for the backend in language X, using ORM Y, one for the frontend using Backbone. A lot of validation in common, same sh*t for generating urls, etc … I just started with Node.js, and I thought that since you can run a lot of the same code on the backend and frontend, why not using Backbone.js models for the backend as well, so that there would be less code duplication ? I couldn’t find info about that … any pointers ? Is it a bad idea and why ? I can only guess that in that case you’d have to write your Backbone.sync for database persistence.
EDIT
Practical issues that need to be solved to achieve that :
Backbone.syncto work with mongodb (or whatever database is used).- building the client-side javascript file with node.js files, possible solution : browserify
After trying out for a while building the backend with
Backbone, I found it was total overkill, and brought more problems than simplifications. Instead I rolled back to using simple wrappers aroundmongodb.My initial thought in using Backbone was sharing url generation and model validation between backend and frontend. But as a matter of fact you don’t need Backbone for that.
So I wrote reusable functions for validation and url generation, and used
browserifyto make them available client-side.Of course this might all change in the future if more people build tools for running Backbone backend-side … but at the time of the writing, Backbone is definitely not made for that, and there’s no library to help adapt it.