收藏    我在用   0 人在用
 -  null

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

 GitHub     下载     同步

46    60    9    over 2 years前


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.

Inject some constant data on the server and mount sharify

var sharify = require('sharify'); = {
  API_URL: '',
  NODE_ENV: process.env.NODE_ENV

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

    //- Adds `` and a convenient `sd` short hand to locals
    if == 'development'
      //- Make sure this is above your other scripts
      != sharify.script()
      script( src='/bundle.js' )

NOTE: Sharify will safely expose the and sd globals to the client-side for the convenience of sharing templates server/client.

Use the shared module server/client

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

app.get('/artwork/:id', function(req, res) {
  new Artwork({ id: }).fetch(//...);
// client.js
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 local

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

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

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) });


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



No Reply at the moment.
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up