Easily share data between modules meant to run on the server and client using browserify.
Easily share data between Browserify modules meant to run on the server and client.
The following example shares a Backbone Model between the server and browser. However, this could be applied to any module shared server/client.
var sharify = require'sharify';sharifydata =API_URL: ''NODE_ENV: processenvNODE_ENV;appusesharify;
var Backbone = require'backbone'API_URL = require'sharify'dataAPI_URL;var Artwork = moduleexports = BackboneModelextendurlRoot: API_URL + '/artwork/';
htmlbody//- Adds `sharify.data` and a convenient `sd` short hand to localsif sharify.data.NODE_ENV == 'development'#debug-modal#scripts//- Make sure this is above your other scripts!= sharify.script()script( src='/bundle.js' )
NOTE: Sharify will safely expose the
sd globals to the client-side for the convenience of sharing templates server/client.
// server.jsvar Artwork = require'../models/artwork';appget'/artwork/:id'id: reqparamsid fetch//...);;
// client.jsvar Artwork = require'../models/artwork'View = require'view.js';model: ;
You can use sharify to bootstrap dynamic data as well.
Inject data into the
var Artwork = require'../models/artwork';appget'artwork/:id'id: reqparamsid fetchreslocalssharifydataARTWORK_JSON = artworktoJSON;resrender'artwork';;;
Require the data on the client
var Artwork = require'../models/artwork'ARTWORK_JSON = require'sharify'dataARTWORK_JSONView = require'view.js';model: ARTWORK_JSON ;
Please fork the project and submit a pull request with tests. Install node modules
npm install and run tests with