A2: easier access to the "site" object in files required from app.js

9 views
Skip to first unread message

Tom Boutell

unread,
Mar 11, 2015, 4:41:37 PM3/11/15
to apostr...@googlegroups.com
A lot of us are building impressive configuration objects to pass to
apostrophe-site, like this:

var site = require('apostrophe-site')({
pages: {
load: [
quiteALotOfFunctions
]
}
});

It's natural to want to access things like site.apos.get inside those
functions. And you can do it, because "site" is defined here in
app.js.

But it's also natural to want to break up app.js into multiple files
using require. And right now, you can't really do that with functions,
because at the time "require" is called, "site" doesn't exist yet.

We've just published a fix for this problem. You can now write:

var site = require('apostrophe-site')();

site.init({
pages: {
load: [
quiteALotOfFunctions
]
}
});

By breaking the creation and configuration of your site object into
two steps, we make it easy to use require:

var site = require('apostrophe-site')();
site.init({
// ... regular stuff ...
pages: {
load: require('./lib/loaders.js')(site)
}
});

// in lib/loaders.js

module.exports = function(site) {
return [
function(req, callback) {
site.apos.doSomethingInteresting(callback);
}
]
};

Hope you find this as helpful as we do!

--


THOMAS BOUTELL, DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

Fotis Paraskevopoulos

unread,
Mar 12, 2015, 4:38:19 AM3/12/15
to apostr...@googlegroups.com
Wonderful!!! 

--
You received this message because you are subscribed to the Google Groups "apostrophenow" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apostropheno...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages