Use method from library.js in main.js ( static js file )


  • Hi,

    I have a problem while using Quickstart Plugin for NodeBB
    In library JS I wan to add:

    plugin.testMethod = function () {
    	console.log('test method');
    };
    

    or:

    const User = module.parent.require('./user');
    plugin.getUserData = User.getUserData;
    

    how I can make use of it in main.js ?
    In

    $(document).ready(function () {
    

    thanks

  • Swedes

    I don't believe you can do that since what you're asking for is using server side code on the client. To do that, the entire stack would need to be available on the client and everything it does. Basically running NodeBB on the client.

    What you could do is set up your own socket and return the desired data that way.

    In your library.js file:

    const pluginSockets = require.main.require('./src/socket.io/plugins');
    
    // This will add to the sockets object used by NodeBB core
    pluginSockets.customFunc = ((socket, dataDoToFetch, done) => {
    	const response = 'This response is only a string but it can be whatever you want';
    	done(null, response);
    });
    

    Then client side:

    socket.emit('plugins.customFunc', dataDoToFetch, (err, data) => {
    	if (err) console.log('My socket customFunc', err, data);
    	else console.log('Do stuff with this', data);
    });
    

    I cannot guarantee that this will work out of the box since my setup involves a bit more complexity, but this is the basic idea. Give it a shot!

Suggested Topics

  • 1
  • 3
  • 6
  • 3
  • 3
| |