Benchpress is undefined clien side

Moved NodeBB Development
  • Re: help with using helpers inside my theme

    So I found this topic that helped someone getting helpers to the client. Unfortunately I'm getting TypeError: Benchpress is undefined client side. I followed the instructions in the topic and have installed benchpressjs in my packages (for the theme), but no cigar.

    Anyone know what I'm doing wrong?

  • What exactly did you try?

  • So I had similar issues as the original post. The helpers run fine server side. but not on the client. If I have a helper on the profile page it works fine if I type the address and browse to it, but if I navigate to the profile the helper does not run.

    What I tried is what was written in the forked post.

  • It's essentially the same code we use in core: https://github.com/NodeBB/NodeBB/blob/master/public/src/modules/helpers.js#L9

    So I don't know why you'd encounter this undefined issue. I assume you've tried rebuilding and restarting. Maybe try wrapping all of that in $(document).ready(function () { ... }); ? Try $(window).load too

  • Thank you @PitaJ for pointing me in the right direction. I adapted my code to the link you sent me.

    (function (factory) {
    	if (typeof module === 'object' && module.exports) {
    		factory(require.main.require('benchpressjs'));
    	} else {
    		require(['benchpress'], factory);
    	}
    }(function (Benchpress) {
    	const logger = (data) => {
    		console.log('Logger helper', data);
    		return '';
    	};
    
    	const customHelpers = {
    		register,
    		logger,
    	};
    
    	function register() {
    		Object.keys(customHelpers).forEach(function (helperName) {
    			Benchpress.registerHelper(helperName, customHelpers[helperName]);
    		});
    	}
    
    	register();
    
    	if (typeof module === 'object' && module.exports) {
    		module.exports = customHelpers;
    	}
    }));
    
    

    Just by doing this made it work client side. But then it stopped working server side. That was easily fixed though by running helpers.register in my library file.

    const helpers = require('./lib/helpers');
    
    helpers.register();
    

    Hope this helpes someone!


Suggested Topics