Getting specific categories through the API

Plugin Development
  • Hi nodebb-community,

    I'm developing a widget that shows categories in material-cards-style on the homepage.

    At the start there were only 4 root-categories, so it looked good. Now there will be 5 root-categories and that won't look good anymore. In library.js my render-function looks like this:

        Widget.renderMaterialCategoriesWidget = function(widget, callback) {
            categories.getCategoriesByPrivilege('cid:0:children', widget.uid, 'find', function(err, data) {
                app.render('categories.tpl', {
                    categories: data,
                    relative_path: nconf.get('relative_path')
                }, function (err, html) {
                    widget.html = html;
                    callback(err, html);
                });
            });
        };
    

    I'm interested in this part: categories.getCategoriesByPrivilege('cid:0:children', ..., ...)
    Can I provide an array of category-ids? Or is there any other way/function I can use to specify the categories I want to render?

  • If you know which cids you want to load you can use Categories.getCategories(cids, uid, next);

  • @baris thanks, that is exactly what I looked for:

            var userProvidedCatIds = widget.data.catIds;
    
            if (!userProvidedCatIds || userProvidedCatIds === '0') {
                categories.getCategoriesByPrivilege('cid:0:children', widget.uid, 'find', render);
            } else {
                categories.getCategories(userProvidedCatIds.split(',').map(Number), widget.uid, render);
            }
    
            function render(err, data) {
                app.render('categories.tpl', {
                    categories: data,
                    relative_path: nconf.get('relative_path')
                }, function (err, html) {
                    widget.html = html;
                    callback(err, html);
                });
            }
    

    Is there any doc-site / rtfm for the JS-API or do we have to look at the source to find appropriate methods and their parameters?


Suggested Topics