javascript - How to share a class development between node module and browser environment -
i have code developped node.js:
user.js:
var underscore = require( '/usr/local/lib/node_modules/underscore' ), backbone = require( '/usr/local/lib/node_modules/backbone' ); var user = backbone.model.extend( { // handle calls mysql }); module.exports = user;
and have code developped browser:
user.js:
define( function( require ) { var backbone = require( "backbone" ); var user = backbone.model.extend( { } ); return luser; } );
is possible share 1 file of user implementation both environment ?
yes, can this. it's bit of tricky problem , there several different attempts solve it. current recommendation approach:
- code exclusively node.js-style commonjs modules
- this means use
require
,module.exports
notdefine
, no wrapper functions, etc
- this means use
- use browserify package bundle of code , ship browser
- use packages npm whenever available (backbone, underscore, etc), otherwise use browserify's support non-npm libraries shim things needed (angularjs, etc)
use available tutorials online. take effort hang of things, once you're on learning curve, you'll able share , reuse code pretty effectively.
note code snippet above: require modules name when available in npm, or relative path when needed, never use absolute path have above.
Comments
Post a Comment