From a718f34c589be87a14a6a9b1a1a759d2adc62632 Mon Sep 17 00:00:00 2001 From: Dave C-J Date: Thu, 18 Sep 2014 17:06:35 +0100 Subject: [PATCH] Expand the non-functional sample to have an input to make it more useful --- nodes/99-sample.html.demo | 2 +- nodes/99-sample.js.demo | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/nodes/99-sample.html.demo b/nodes/99-sample.html.demo index 6a9f7e839..4dcc8ba5b 100644 --- a/nodes/99-sample.html.demo +++ b/nodes/99-sample.html.demo @@ -65,7 +65,7 @@ name: {value:""}, // along with default values. topic: {value:"", required:true} }, - inputs:0, // set the number of inputs - only 0 or 1 + inputs:1, // set the number of inputs - only 0 or 1 outputs:1, // set the number of outputs - 0 to n // set the icon (held in icons dir below where you save the node) icon: "myicon.png", // saved in icons/myicon.png diff --git a/nodes/99-sample.js.demo b/nodes/99-sample.js.demo index 514b98a8b..02a0abf6e 100644 --- a/nodes/99-sample.js.demo +++ b/nodes/99-sample.js.demo @@ -20,16 +20,18 @@ module.exports = function(RED) { - "use strict"; - + "use strict"; + // require any external libraries we may need.... + //var foo = require("foo-library"); + // The main node definition - most things happen in here function SampleNode(n) { // Create a RED node RED.nodes.createNode(this,n); - + // Store local copies of the node configuration (as defined in the .html) this.topic = n.topic; - + // Do whatever you need to do in here - declare callbacks etc // Note: this sample doesn't do anything much - it will only send // this message once at startup... @@ -37,19 +39,26 @@ module.exports = function(RED) { var msg = {}; msg.topic = this.topic; msg.payload = "Hello world !" - + // send out the message to the rest of the workspace. this.send(msg); - + + // respond to inputs.... + this.on('input', function (msg) { + node.warn("I saw a payload: "+msg.payload); + // in this example just send it straight on... should process it here really + this.send(msg); + } + this.on("close", function() { // Called when the node is shutdown - eg on redeploy. // Allows ports to be closed, connections dropped etc. // eg: this.client.disconnect(); }); } - + // Register the node by name. This must be called before overriding any of the // Node functions. RED.nodes.registerType("sample",SampleNode); - + }