mde/geddy — pull request #516 by percygrail (2013-12-17 22:20:39+00:00)
Title: Add a standard way to create js module for client and server side use

Emulating the way the models are bundled for client side use, I've updated build.js to bundle up any xxx.shared.js files in app/helpers and publish them to public/js/core/helpers.js.

To test, create a file called test.shared.js in app/helpers and add this code to it:

if(typeof exports == 'undefined'){
var exports = this['helpers'] = {};
}
exports.testSharedHelpers = function() {
return '---------------Testing shared helpers---------------';
};

Add this line to the appropriate place in app/views/layouts/layout_header.html.ejs (or whatever templating you are using):

<%- scriptLink('/js/core/helpers.js', {type: 'text/javascript'}) %>

Add this require and line of code somewhere server-side (I used config/init.js):

var sharedHelperTest = require('../app/helpers/test.shared.js');
...
console.log(sharedHelperTest.testSharedHelpers());
...

Start geddy and look for the console.log output server side.

Load up your app in a browser and type the following at the js console prompt:

console.log(helpers.testSharedHelpers());

Look for the console.log output client side.

If you like this functionality, I hope you will comment on the following:

1. Should I add the scriptLink to all the layout header templates or leave it like it is and let the developer add it as needed?

2. If so, should I use gen/base/realtime/views/xxx/layouts/layout_header.html.ejs or gen/base/views/xxx/layouts/layout_header.html.ejs

3. This whole thing could also be done by adding a new directory somewhere (app/shared_helpers?) and bundling everything in there (as opposed to specially named files in app/helpers), but this would require touching generator code and may have ramifications beyond a geddy noob's (that's me) understanding.

4. Is the way I've done this tied up with realtime in such a way that it is not available without turning on realtime (and if so, does anyone care)?