JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 836
  • Score
    100M100P100Q120881F

Easily share data between modules meant to run on the server and client using browserify.

Package Exports

  • sharify

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sharify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

sharify

Easily share data between modules meant to run on the server and client using browserify.

Example

The following example shares a Backbone Model between the server and browser. However, this could be applied to any module shared server/client.

Inject some constant data on the server and mount sharify

var sharify = require('sharify');
sharify.data = {
  API_URL: 'http://artsy.net/api/v1',
  NODE_ENV: process.env.NODE_ENV
};
app.use(sharify);

Use in a module that can run on the server or client

var Backbone = require('backbone'),
    API_URL = require('sharify').data.API_URL;

var Artwork = module.exports = Backbone.Model.extend({
  urlRoot: API_URL + '/artwork/'
};

Inject sharify script in the view

html
  body
    if sharify.data.NODE_ENV == 'development'
      #debug-modal
    #scripts
      //- Make sure this is above your other scripts
      != sharify.script()
      script( src='/bundle.js' )

Use the shared module on the client

var Artwork = require('../models/artwork'),
    View = require('view.js');

new View({ model: new Artwork() });

Bootstrapping Request-level Data to the Client

You can use sharify to bootstrap dynamic data as well.

Inject data into the sharify.data local

var Artwork = require('../models/artwork');

app.get('artwork/:id', function(req, res, next) {
  new Artwork({ id: req.params.id }).fetch({
    success: function(artwork) {
      res.locals.sharify.data.ARTWORK_JSON = artwork.toJSON();
      next();
    }
  });
});

Require the data on the client

var Artwork = require('../models/artwork'),
    ARTWORK_JSON = require('sharify').data.ARTWORK_JSON,
    View = require('view.js');

new View({ model: new Artwork(ARTWORK_JSON) });

Contributing

Please fork the project and submit a pull request with tests. Install node modules npm install and run tests with make test.

License

MIT