Saving settings back to the server

Solved Plugin Development
  • In the documentation, the Setting example mentions doing: socket.emit('admin.settings.syncMyPlugin');".

    I am guessing it requires me to implement some code on the back-end. Can anyone shed some light on how to save back to the settings? Isn't just a matter of setting it and syncing it? Why the socket operation?

  • Right now it causes the following to happen:

    13/11 11:21 [93965] - warn: [socket.io] Unrecognized message: admin.settings.syncMyPlugin
    
  • The settings page could a bit of editing, it took me quite a while to understand everything that's happening.

    On the server, you load the settings with something like:

    var Settings = module.parent.require('./settings');
    var mySettings = new Settings('myPlugin', '0.1', defaultSettings, function(){
      console.log(mySettings.get('somesetting'));
    });
    

    This will load the settings from the database into the mySettings object, but will not update them after they change.

    To update the the mySettings object, you have to do sync:

    mySettings.sync(function () {
      console.log(mySettings.get('somesetting')); // Updated
    });
    

    Now on the client, when you do settings.persist(), the new settings are saved to the database, but the mySettings object on the server is not updated, so we need do a socket.emit() to let the server know that it needs to sync the mySettings object.

    
    // Client admin page
    
    require(['settings'], function (settings) {
        var wrapper = $('#my_form_id');
    
        // This loads the data from the database to the DOM.
        settings.sync('myPlugin', wrapper);
    
        $('#save').click(function(event) {
            event.preventDefault();
    
            // This saves the settings from the DOM to the database.
            settings.persist('myPlugin', wrapper, function(){
    
                // When we're done saving to the database, send a socket event to server so it can update it's mySettings object with the new values.
                socket.emit('admin.settings.syncMyPlugin');
            });
        });
    });
    
    // On the server
    
    var SocketAdmin = module.parent.require('./socket.io/admin');
    
    // Socket event exposed to the client as 'admin.settings.syncMyPlugin'
    // It gets called when the client saves new settings to the database.
    SocketAdmin.settings.syncMyPlugin = function() {
        mySettings.sync(function () {
    
            // The mySettings object is updated, you can now call whatever functions you want to based on the new values.
            if (mySettings.get('somesetting') === 'justDoIt') doThing();
        });
    };
  • Very complete answer, thank you.


Suggested Topics


  • 0 Votes
    6 Posts
    622 Views

    I can tell u what runFunction() is, I just simplified it for a better understanding.
    Because what I just to replace is this condition if( $.inArray( parseInt(ajaxify.data.cid, 10) , CategoriesIds) != -1) instead of going search if the current category is on the array CategoriesIds above (hardcoded), I need to check if the current category is on the selection made by the user on the plugin config page.

    And I'm not realizing how I make that connection between the plugin settings and the function.

    I don't know if I'm making sense to you, but here is the repository with all the code: https://github.com/jtsimoes/nodebb-plugin-nsfw

  • 0 Votes
    2 Posts
    998 Views

    You need to run event.preventDefault() to prevent the Click from automatically submitting the form

  • 0 Votes
    2 Posts
    1k Views

    @Thadeusz-Lay so are you trying to create your own plugin? meta is a NodeBB module. You have to require it from NodeBB with require.main.require('./src/meta') on the server side.

  • 0 Votes
    1 Posts
    1k Views

    Suppose there are two files.

    library.js in the /nodebb-plugin-namePlugin
    file.js in /nodebb-plugin-namePlugin/static/lib

    Now I must do a lot of operation in my library.js and I must check when user is created. So I use the hook and It's everything ok, but, after I must my check in library I need to send a message to my file.js to change background color of a component. The proble is that I used this to communicate:

    websockets = module.parent.require('./socket.io'), //library.js websockets.in('uid_' + data.uid).emit('hash', { 'username': data.data.username }); //file.js socket.on('hash',function(){ console.log("DOG); });

    The problem is that I don't have data.uid value because I'm checking before that an user can registrate himself. Anyone can help me to use websocket without uid or can suggest me other methods?

  • 0 Votes
    4 Posts
    2k Views

    Always makes me smile when I can help.
    Your plugin also opened nodeBB to a more scientific audience, which I personaly find extra apealing. 🙂