How to call javascript functions

  • Plugin & Theme Dev Anime Lovers GNU/Linux

    Umm I've been at this for three hours, im done. how do i call custom javascript functions on NodeBB without writing a plugin? I do not want to write plugins because it would take too long for what I'm doing. I want to directly inject in a JS file and put it in the header or somewhere (preferably the footer because of the parallel thing) granted its jQuery compatible and its just not working at all. 😞

    Works when I use the console though...


  • Have you debugged to make sure the file is getting loaded in the browser? Another way to find out would be a alert() in the js somewhere.

    Also after editing tpl's for footer are you restarting nodebb?

  • Plugin & Theme Dev Anime Lovers GNU/Linux

    Currently running forever with the -w command so a restart is not necessary as it automatically restarts itself.

    I've debugged it and there are no problems, the file loads perfectly fine, but the function I have in there doesn't work.

    (function(global){'use strict' ...}
    ...
    $("#blah").blah();
    

    When I put $("#blah").blah(); in the console to force it it works, but it doesn't work on load is what I'm trying to say.

  • Admin NodeBB

    How are you adding the js code to nodebb?

    If it is a stand alone js file did you include it in the header like :

    <script src="{relative_path}/socket.io/socket.io.js"></script>
    

    You can also use requirejs like we do then would have to include it like this :

    https://github.com/designcreateplay/NodeBB/blob/master/public/src/forum/topic.js#L1

    **Edit: **

    If you want something to run on page load then you can put it inside :

    $(document).ready(function() {
          alert('foo');
    });
  • Plugin & Theme Dev Anime Lovers GNU/Linux

    That did it, thanks Baris!

  • Plugin & Theme Dev

    @trevor may I ask how exactly are you injecting your code? or in another word, where? which file are you editing, header|footer.tpl?

    I'd like to do so in a cleaner way, and since it's custom code, how to safely keep the file in the NodeBB dir without risking loosing it or being overwritten when you update? can i use the /public/ dir for custom assets?

    I did something similar using a plugin is to use the filter:footer.build hook, and inject script tag in there, without the need to modify any NodeBB native code.

    plugin.json

     "hooks": [
      { "hook": "filter:footer.build", "method": "footer", "callbacked": true }
    ]
    

    plugin.js

    ....
    Plugin.footer = function(custom_footer, callback) {
      custom_footer = custom_footer + '';
      custom_footer += '<script src="path/to/script/god.js"></script>';
      callback(null, custom_footer);
    }
    ...
    

    Also, as far as I know, generally, NBB plugins execute on the server, (unless they inject them selves into content, which is kinda what i did) and not on the client, it seems as your code is client side.

  • Admin

    I think @trevor is modifying the templates and js files as is. So everytime we make a major upgrade he has to go through the pain of manually upgrading his files by hand.

    Part of my long term goal here is to find some sort of solution to being able to customize templates and functionality without being totally screwed everytime we overhaul a template or client side JS.

    I'm open to solutions... but I have an idea. I'm hoping to be able to implement something solid before we hit 0.3.x. 🙂

  • Plugin & Theme Dev

    @psychobunny ahh ... I am not sure I want to do that.. it would be nice if we can override some templates, or augment them, maybe in the nodebb-themes

    and while you're there, i feel like template.js can be abstracted (just like the awesome work @baris did for DBs) to use a templating engine of choice, handlebars || jade || ejs, no offense, I like your dcp.template, but it's too light, lol, or just add an <!-- ELSE --> inverse block, pl0x

  • Admin

    there is an else conditional, see
    topics.tpl:27,
    account.tpl:38,
    category.tpl:67,
    etc.

    that said, I agree with you, as I think I said on the dcp.templates issue thread 🙂 there should be a way to abstract out template parsing to allow various engines

  • GNU/Linux Admin

    to use a templating engine of choice, handlebars || jade || ejs

    You're preachin' to the choir there... I've been barking up that tree for months.

  • Admin

    inb4

  • GNU/Linux Admin

    Hahaha... time to backpedal. I love Andrewbars... solves all world problems. 😄

    There are some nice advantages to rolling your own template engine, of course... don't get me wrong.

  • Admin

    he only said this because I threatened him on skype

  • Plugin & Theme Dev

    hahaha
    I judged dcp.template too quick, i could swear I Ctrl+F in templates.js and didn't see an ELSE, so I gave up then 😛

    Yea, I know we talked about this before, that just was my friendly reminder, didn't mean to start a skype-war

  • Admin

    yeah haha. I don't know, I guess templates.js was an adventurous thought of being able to import themes from old school forums.

    Now that I think about it, probably bit off more than we could chew. So yeah definitely open to some sort of abstraction, although I don't imagine that will seriously happen until a lot of interest comes up in the realm of theming NBB

    As for skype wars? this happens everyday, all the time. the winner is usually the one that posts the most convincing meme

    meme

Suggested Topics

| |