custom loader for apostrophe-snippets without the pain of creating a whole module
I need a module that has 99% the functionality of apos-snippets, only it does some "featured article" kind of thing on its' index.
I'd like to implement a loader specific to my extension of apostrophe-snippets.. What's the easiest painless way?
Can you explain the featured article thing a bit more?
Typically, we've written our module specific loaders in the index.js of the lib/module directory for that module.
First, to give you a straight answer: if a module has a "loader" method, apostrophe-site wires it up as a page loader. So there you go.
However, the apostrophe-snippets method already has a loader method that implements a simple pattern for dealing with index views, "show" views, adding criteria to the query, etc. And in most cases you're way better off overriding one of the methods that the loader invokes.
The standard loader starts by doing joins and adding permissions info to req.extras, then it invokes self.dispatch to decide what view to render and fetch content.
The less you override the less you'll have to reinvent (:
Here are two low-impact ways to build what you have in mind. The second one takes advantage of overriding the addCriteria method. The first one is even simpler.
Also see the README for apostrophe-snippets, which is actually pretty solid and covers a lot of other override options. (Maybe too many in one place! Our upcoming doc site breaks these tutorials up a lot more.)
OK, I'm going to assume that your module is called "stories" and it's set up like this in app.js:
stories: {
extend: 'apostrophe-snippets'
}
But my advice here still works if it's subclassed from apostrophe-blog.
OPTION ONE: a widget
Option one is to just override index.html by creating lib/modules/stories/views/index.html, and include a widget at the top:
{{ aposSingleton(page, 'featured', 'stories') }}
Possibly checking for a page number so you don't annoy people who are deliberately looking for page two of the regular content:
{% if query.page < 2 %}
{{ aposSingleton(page, 'featured', 'stories') }} {% endif %}
Your user can use the widget to pick some featured stories, or set it to show stories with the "featured" tag.
OPTION TWO: change the sort
The widget is really easy but it does mean redundancy is possible with stories appearing both in the widget and in the "regular" list below it. If you prefer you can change the sort to push featured stories to the top of the regular list. This is probably what you were originally thinking to do.
For this you might add a boolean property to denote featured-ness, when configuring in app.js:
stories: { addFields: [ { name: 'featured', label: 'Featured', type: 'boolean' } ] }
Now you can decide whether a story is featured when editing it.
Then extend the addCriteria method, like this:
var superAddCriteria = self.addCriteria;
self.addCriteria = function(req, criteria, options) { // Do the usual stuff superAddCriteria.call(self, req, criteria, options); // Now change the sort options.sort = { featured: -1, publishedAt: -1 } };
This puts featured stories first, then regular stories.
self.addCriteria is called by self.dispatch, which is called by the loader.
On Apr 24, 2014 6:29 AM, "Danny Povolotski" [email protected] wrote:
I need a module that has 99% the functionality of apos-snippets, only it does some "featured article" kind of thing on its' index.
I'd like to implement a loader specific to my extension of apostrophe-snippets.. What's the easiest painless way?
— Reply to this email directly or view it on GitHubhttps://github.com/punkave/apostrophe/issues/113 .