Helper function not working at initial page load

Moved Technical Support
  • Hi community, I am working on customizing the persona theme, and I need to override the generateChildrenCategories helper that comes with NodeBB. I successfully overrode the function and it's rendering my customized subcategories.

    BUT it only works if I click on any of the links that take me to /category . If I refresh the page, the helper function does not work (not called or not yet initialized I guess), it renders no subcategory icon at all. Same happens when first landing at the forum as well.

    Here is my code
    library.js

    library.init = function(params, callback) {
    	...
    	templates.registerHelper('myGenerateChildrenCategories', function (category) {
    		...
                    return html;
    	};
    
    	callback();
    };
    

    persona.js

    $(document).ready(function() {
            ...
            templates.registerHelper('myGenerateChildrenCategories', function (category) {
    		...
                    return html;
    	};
            ...
    }
    

    item.tpl

    <!-- IF !config.hideSubCategories -->
    {function.myGenerateChildrenCategories}
    <!-- ENDIF !config.hideSubCategories -->
    
  • @genesisx sounds like the server-side helper is not registered correctly. Maybe try adding a console.log near it to confirm the code is being executed?

  • @PitaJ Thanks, I will try this tonight, is library.init the right place to register a helper function? I also tried to put it in appLoad hook , got same behavior.

  • @genesisx init isn't special, it's just the normal method name we use for the static:app.load hook

  • @PitaJ Found a syntax error after looking carefully at the server side code, thanks for pointing out the problem is at server side...thought I copy pasted it! Thanks again! 😄


Suggested Topics