Reputation: 321
Looking for a solution for a few days. Need help.
Source: node.js + express.js + jade template engine
Problem: Can't understand how I can render 1+ dynamic blocks on one page.
For example:
We have a page: News main page Blocks on page: Latest news (list 20 itens), hot news (list 4 items), most viewed news (4 items), block with news categories (it can display current category on the page with page with card of one selected novelty, so it is dynamic block too), and block with some user auth data. "block" i mean a widget as we can see on site, not a block of code.
What can I do in express? I can route special url to special function in routes. So as I see, if a want to render all this blocks on the one page I have to call all functions rendering each block in only one function of route.
I mean it seems that I have to do something like this (sure in libs but doesn't matter here)
app.get('/news', function(req, res){ call_last_news(funcion(){ call_hot_news(function(){ call_get_user_info(function(){ ... ... ... template.render.here(); final_here(); }); }); }); });
This looks real but so unuseful and unsupportable code that .. That's bad. I can see solution in calls from template engine to render some blocks on the page. But not just include because all blocks can use db or cookies, session data etc. all blocks are dynamic. But I have no idea how to create such engine using express.js + jade
Upvotes: 0
Views: 1304
Reputation: 64312
Well I'm not certain I understood all of the problem but based on the pseudo code, it looks like the main concern here is that you have a deeply nested set of potentially independent and reusable functions. There are two approaches to this problem that come to mind:
Use something like async series, parallel, etc. (which function depends on the nature of your code – are all of the call_
functions independent?) That will clean up your code and make it more maintainable.
Another approach you can take is to quickly render the page without all of the call_
functions and just make several ajax calls from the client to fill in the data. You could have routes which look like '/news/last', and '/news/hot', etc. This is nice because you can separate out all of the logic for each of these units into reusable URLs so you can mix and match them on any page.
Upvotes: 0