Accessing context/flow/global from custom nodes

802 views
Skip to first unread message

Julian Knight

unread,
Jan 31, 2016, 10:50:30 AM1/31/16
to Node-RED
Hi,

Is it possible yet to access the new contexts from our own custom nodes?

Regards, J.

Nicholas O'Leary

unread,
Jan 31, 2016, 10:58:54 AM1/31/16
to Node-RED Mailing List
Yes - the Switch/Change nodes have no special privilege, they use the same api as all nodes can use.... but it looks like I forgot to write the page in the docs on that...

The node should do:

    var myContext = this.context();

    // Access the node's private context
    var fred = myContext.get('fred');
    myContext.set('fred',123);

    // Access the flow context
    var fred = myContext.flow.get('fred');
    myContext.flow.set('fred',123);

    // Access the global context
    var fred = myContext.global.get('fred');
    myContext.global.set('fred',123);


If you want to take advantage of the new TypedInput widget in your node, you'll have to wait for the full docs....

Nick


--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Julian Knight

unread,
Jan 31, 2016, 12:12:50 PM1/31/16
to Node-RED
Fab. Thanks Nick.

One other small thing. Is there a recommended way to make a custom node dependent on a specific version of Node-Red? I'm thinking that the current version of my context get/set node should be made to one work with NR v0.12 or below. I'll make a new (breaking) version but I'd like to make that dependent on NR v0.13+

Nicholas O'Leary

unread,
Jan 31, 2016, 12:16:32 PM1/31/16
to Node-RED

Yes, there's a custom property you can add to package.json to specify what version if NR your node works with. Doesn't help the user install the right version, but stops it being loaded if there's a mismatch. It got added in 0.10 iirc.
But until I get back to my laptop I can't tell you exactly what it is.

Nick

Reply all
Reply to author
Forward
0 new messages