I'm beginning learning Node.js and Backbone. I'd like to share some code between the server and the client (related to [1] and [2]). More in deep:
So my idea is to use a factory object to encapsulate common code (defaults, validation rules, public instance methods) and adapt the factory object based on the environment (Node.js or AMD module).
This code is quick&dirty and may not work. Is this a good approach or just a waste of time?
(function (factory) {
if (typeof exports === 'object') { // Node.js
var config = require('../config/config.json'),
Sequelize = require('sequelize'),
_ = require('underscore');
var User = {
receiveSms: {
type: Sequelize.BOOLEAN, allowNull: false
},
receiveNewsletter: {
type: Sequelize.BOOLEAN, allowNull: false
}
};
// Add defaultValue for each returned by
_.each(factory.defaults, function (value, key) {
if(_.has(User, key)) {
User[key].defaultValue = value;
}
});
module.exports = function () {
(new Sequelize(config.database, config.username, config.password))
.define('User', User);
};
} else if (typeof define === 'function' && define.amd) { // AMD
define(['backbone', 'uderscore'], function (Backbone, _) {
return Backbone.Model.extend(factory);
});
}
}(function () {
return { // To be adapted
defaults: {
receiveSms: false,
receiveNewsletter: true
}
}
}));
I think it's a better solution to use require.js (especially for using frameworks like underscore in multiple files). You should use a factory only for objects that change during the runtime. A shopping cart for example (but even in this example i think it's more appropriate to use a backbone collection that is given to your function as an argument when instantiated). See more information here: http://requirejs.org/docs/node.html
Personally, I would steer away from requirejs as it requires you to rewrite your modules to fit their spec, which will eventually become defunct as the Ecmascript standard evolves.
For the time being, I would advise looking into the 'gulp' streaming build system. Using it, you'll find it extremely easy to pipe your shared js scripts into a publicly accessible directory, which can then be loaded from the client-side. A common technique for converting node js modules into browser scripts is using 'browserify' - magic!
Process: gulp.src > gulp-browserify > gulp.dest > ??? > Profit.
Further reading: http://viget.com/extend/gulp-browserify-starter-faq