javascriptnode.jsdnode

How do i make multiple Dnode connections?


I'm doing an autocomplete input form and i want to send what my user types to a remote database for suggestions.

I use dnode for it now, and i do not want to do a new remote connect each time my user types so i made the remote function global like this

dnode.connect(5050, function (remote) {
    window.remote = remote

});

So each time i want to check my mongodb i just use the window.remote.function and dont have to reconnect. Are there a better way?

Thanks


Solution

  • Bind your autocomplete listeners inside of the scope of the dnode connection instead of exposing the connection to the outside.

    Instead of doing:

    dnode.connect(5050, function (remote) {
        window.remote = remote
    });    
    autoCompleteLogic(window.remote)
    

    do this instead:

    dnode.connect(5050, function (remote) {
        autoCompleteLogic(remote)
    });