Skip to content

Instantly share code, notes, and snippets.

@craigspaeth
Last active January 1, 2016 03:59
Show Gist options
  • Save craigspaeth/8088618 to your computer and use it in GitHub Desktop.
Save craigspaeth/8088618 to your computer and use it in GitHub Desktop.
Sharing data between Browserify modules
// I have a Backbone model where I need to use the same API_URL variable
// on the server and client. With Sharify it looks something like this...
// app.js
sharify.data = { API_URL: 'http://artsy.net/api/v1' };
app.use(sharify);
// models/artwork.js
var Backbone = require('backbone'),
API_URL = require('sharify').data.API_URL;
var Artwork = module.exports = Backbone.Model.extend({
urlRoot: API_URL + '/artwork/'
};
// With express-state I can bootstrap that variable into a global on
// `window`, but to share that variable on the server I would need to
// expose it globally as well.
// app.js
expstate.extend(app);
app.expose({ API_URL: : 'http://artsy.net/api/v1' }, 'SHARED_DATA');
global.SHARED_DATA = app.locals.state; // This is what I wanted to avoid with Sharify
// models/artwork.js
var Backbone = require('backbone');
var Artwork = module.exports = Backbone.Model.extend({
urlRoot: SHARED_DATA.API_URL + '/artwork/'
};
// That being said, maybe one global namespace in Node isn't that big of a deal.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment