Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Allow setting a global javascript object for the client (through the server) #353

Closed
mdedetrich opened this Issue Feb 12, 2013 · 5 comments

Comments

Projects
None yet
4 participants

This is more of a feature request, however its something that is in (my opinion) required, at least for the work I do which involves using Facebook's javascript SDK on the client side

You should basically be able to set a javascript object (in JSON) format on the server, which is than accessible in browser clients

As an example, when you do something like this

ss.client.define('main', {
    view: 'app.jade',
    css: ['common.styl'],
    code: ['system', 'libs/tinybox2.js', 'libs/jquery.js', 'libs/backbone.js', 'libs/facebook.js', 'app'],
    tmpl: '*',
    clientObject: {
        someVal: 1,
        anotherVal: "cheese"
    }
  });

Then in the browser you would be able to access this object (through something like ss.clientObject)

As mentioned earlier, this would be incredibly helpful for my case due to using facebook's javascript sdk, so it would look something like this (for my case)

ss.client.define('main', {
    view: 'app.jade',
    css: ['common.styl'],
    code: ['system', 'libs/tinybox2.js', 'libs/jquery.js', 'libs/backbone.js', 'libs/facebook.js', 'app'],
    tmpl: '*',
    clientObject: {
        facebookAppID: config.get('facebookApplicationID')
    }
  });

And I would be able to access the facebookApplicationID through the client (and feed it into the SDK). I am sure there are other reasons to use something like this

Currently I have to manually change JS files (in this case the facebook.js) every time I deploy, and change it back when I work locally

owenb commented Mar 13, 2013

Hi there

This is a good idea. Let me have a think about the exact API

Contributor

hathvi commented Mar 14, 2013

It would be nice if we could just define arguments that get passed into entry.js

The client definition could look something like this

ss.client.define('main', {
  view: 'app.html',
  css: [ 'app.less' ],
  code: [ 'libs', 'app' ],
  onEvent: function(req, res) {
    var config = {
      facebookAppID: config.get('facebookApplicationID')
    };

    res(config, req.session.userId);
  }
});

While entry.js looks like this

module.exports = function(ss, config, userId) {
  // config.facebookAppID == '1251251251251235';
  // userId == '511ee60fc404a93c0a00000e'

  ss.server.on('ready', function() {
    // ...
  });
};

owenb commented Mar 14, 2013

We can't do this dynamically each time we serve the view as we want to be able to cache it in production (or on a CDN), so sending a userID is a no go.

But I'm all for pushing 'static' config params to the client somehow. Will bear it in mind when working on 0.4.

@thepian thepian self-assigned this Nov 17, 2014

@thepian thepian added this to the 0.4 milestone Feb 1, 2015

Owner

thepian commented Mar 13, 2015

We now have a branch with a solution

  • view specific constants can be defined in the defintion. They will be global objects in the browser.
  • global constants can be defined with ss.client.send('constant', 'name', 'value')
  • documented
  • constants are passed to formatters
  • locals map supported as well for tempting information

TODO:

  • template_engine
  • more tests
  • disable live_reload during tests (runs out of handles)
  • document recommendations for writing formatters

@paulbjensen can you have a quick look if this seems Ok

Owner

thepian commented Mar 14, 2015

@mdedetrich How does this match what you were doing?

@thepian thepian closed this Mar 15, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment