Passing custom data into a template?


  • @baris thanks.. that is a completely different plugin.json layout.

    maybe I started with an old module format..

    it WAS working yesterday.. so I'll go back to that commit and see if I can find the difference


  • @baris this page shows the format required by the 2.4.5 version I am on..

    it is similar to mine

    also, my admin.js IS loaded and run

    as the messages from the run are shown at the client
    (browser dev winbow console)

    /plugins/post_link_list admin define running
    /plugins/post_link_list admin define ending returning acp keys= ['init']

    but the actual init() method is not called.

    adding the module entry to plugin.json solved the error and the init is now called...

  • NodeBB Admin

    Yeah you need your admin.js file in the modules section, acpScripts is for scripts that will be run whenever the admin site is loaded, think of them as global javascript that you want to include on the ACP site. modules section is for new pages for plugins or custom js modules. They are loaded on demand when the user navigates to your page.


  • @baris ok, thanks. but none of that makes sense.. sorry.. 'user' is only admin in my case.

    added on the ACP site (for what purpose if not for when the user navigates)?

    i don't want it to run twice.. so it sounds like do NOT use acpScripts..

    but. back on the custom data to the template

    I see two different approaches used.. (neither work for me)

    some modules use admin to use the settings api to load the values into a css class using jquery $('.someclassname')
    which the template uses somehow, but unclear ${'../name'}
    https://github.com/oplik0/nodebb-plugin-category-queue.git

    admin.js
    Settings.load('category-queue', $('.category-queue-settings'));
    template
    			<form role="form" class="category-queue-settings">
    				<!-- BEGIN categories -->
    				<div class="form-group col-sm-4 col-xs-6">
    					<label for="{../cid}">{../name}</label>
    

    and the other passes data in the call to res.render(page, {data structure})
    in lib/controllers/renderAdmin()
    then the template uses the data structure elements clearly.

    my data is an array.. no internal named elements (presumably that is the ${../name} thing...accessing the object elements
    and there is no for loop construct in the template, so I don't see how it generates 4 instances in the output...

    sorry, one more question on templates.. is there any way to test them outside trying to load the plugin.. cause they are not generating the content I expect, but don't see any errors during build or run

  • NodeBB Admin

    In your plugin's case you would need to move your admin js file to the modules section like so

    "modules": {
      "../admin/plugins/post_link_list.js": "static/lib/admin.js",
    }
    

    And remove it from acpScripts.

    When you do this static/lib/admin.js will be loaded and its init method will be called when the user(admin) navigates to your plugin page.

    Now settings.load uses some client side code to load the settings and fill in the input elements on the plugin page. So when that is called from the init function of the admin page. It will populate the forum fields. If you are storing a list of urls I suggest storing them as a string and displaying them in a text area. You can inspect https://github.com/ninenine/nodebb-plugin-beep it does something similar.


  • @baris thanks.. I want to have a field for each url string. (from the array)

    have an input field to add another with a click
    and some selector (minus sign, checkbox...) and click to remove..

    but the templating engine is killing me.. I don't know what the syntax is for non structure objects..

    {{{each post_link_list}}}

    gets me a line for each.. good.. what is its value?

    hm... now it works ok?
    @value says object.. (oops, put in {}), now compiler says use {} as required in 3.x)

    hm.. could use its array index tho...

    this post_link_list comes from the render..

    can I use meta from admin (as module) ? (button click handler) ..
    hm.. can't import meta in admin..


  • @baris well, got almost all of it working, list, add, delete, save button

    now to update the database

    I have the hook on save, so I can reload the operational list and use it to inform the admin page

    it will be loaded via meta.get in the index.js (it doesn't return any data now, as there isn't any)
    but meta isn't usable in admin..

    settings is, but I don't see a save or (or load) method in the src/settings.js
    but from quickstart admin.js
    settings.load('quickstart', $('.quickstart-settings'), function () {

    i want to save from a JS variable. but that causes some error not in the log, and I get the spinning circle saying my plugin is taking a long time.,

    so close..
    the admin code that gets the save click to save

    			var selected = [];
    			$('#urls').find('input[type="text"]').each(function() {
    			    selected.push($(this).attr('value'));
    			});
    			console.log("urls to save=",selected)
    		try {
    			Settings.save(our_keya,selected, (err,sss)=>{
    

    and the dev window console log

    post_link_list save  clicked 
    urls to save= (4) ['github.com', 'pastebin.com', 'forum.magicmirror.builders', 'docs.magicmirror.builders']
    post_link_list settings save executed
    settings save completed
    

    the node bb log capturing from index
    this is the output of setting save(), have the right 'hash', well its the 'key' I sent in
    similar to all the other plugins I looked at

    but no data

    io: 1 on [
      {
        type: 2,
        nsp: '/',
        id: 8,
        data: [ 'admin.settings.set', { hash: 'post_link_list', values: {} } ]
      }
    ]
    

    in index , the hook

    settings saved for  { plugin: 'post_link_list', settings: {}, caller: { uid: 1 } }
    

  • @baris debugging why my settings don't save

    I see the value in the hook now.. the 'value' MUST be a keyed object... not flat

    but still no persistence.

    I see the action hook is called before informing meta to reload..

    but even when I stop and restart nodebb the data is not current

  • NodeBB Admin

    @sdetweil I suggest you take a look at https://github.com/NodeBB/nodebb-plugin-glossary. It has an admin page that saves a list of strings. You can compare it to yours and see why your data isn't being saved.


  • @baris thanks. altho the typical plugin doesn't do it, the settings apis are promisified. you don't wait, u get junk..

    adding .then() in the non async using functions solves the problem


Suggested Topics

  • 16
  • 2
  • 7
  • 1
  • 8
| | | |