Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

NodeJS + JS: generating dynamic links, depending on current user selection

NodeJS + JS: generating dynamic links, depending on current user selection

Problem

I'm trying to create a simple sorting option for posts in my blog. The problem is the last thing I want to use is a JS-based redirections because of the fact some users don't have JS enabled in their browsers, thats why they wont be able to sort posts in me blog. Let me give you an example:

I have several categories in my blog:

//categories selection
  • Music
  • Photo
  • Cinema
  • Computes

Also, I have two sorting options:

//sorting options
Last posts Last comments

For example, lets take that in categories selection 'CINEMA' tab is active at this moment (

  • Cinema
  • ). So, if user clicks Last comments link, it should redirect to

    '/?category=cinema&sort=comments'
    

    Another example: currently Last comments is active (

    Last comments
    

    ). User clicks on Computes in category selection and it should redirect to

    '/?category=computers&sort=comments'.
    

    So, are there any variants for performing such feature without JS? If no, could you suggest me the most elegant solution except manually running over every element in category and sorting optiona and generation link with JS/jQuery?

    Thanks in advance!

    SERVER SIDE:

    db.collection("posts", function (err, collection) {
        //getting posts dependinc on requested category
        collection.find({category: req.query['category']}, {sort: [['time_added', -1]], limit:20}, function (err, posts) {
            //getting posts and rendering page
        });
    });
    

    UPDATE: I've found a so-so solution. For a separate category I render two div's with different posts list. One for 'Last posted', the second - for 'Last commented'. And the sort selector will just hide one div and shows another. The problem is just twice data size transfering to client side. I know, it will require a bit JS, but the main category links would be clear without '#'-hrefs. And sort options - it's just a toogle, it may not have real link.

    Problem courtesy of: f1nn

    Solution

    Seems like you are doing a page refresh for selecting a new category. Use the selected category parameter to determine the URL of the sorting options. Would need some more code from your server side to give you a more concrete example.

    If you don't want to use Javascript you will have to generate the href-attribute of your sorting links Last posts dynamically based on which category is selected. How are you rendering the page today? Just static files? Have a look at a template engine like mustache.js and then you could do it like this:

    Last posts
    

    Have a look at this tutorial for an Node.js example using mustache.js http://mrjaba.posterous.com/a-gentle-introduction-to-nodejs

    Edited after your comment: Or something like this using EJS

    &sort=posts" class="active">Last posts &sort=comments">Last comments
    Solution courtesy of: Charlie Rudenstål

    Discussion

    View additional discussion.



    This post first appeared on Node.js Recipes, please read the originial post: here

    Share the post

    NodeJS + JS: generating dynamic links, depending on current user selection

    ×

    Subscribe to Node.js Recipes

    Get updates delivered right to your inbox!

    Thank you for your subscription

    ×