1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Tiny formatting tidy ups.

This commit is contained in:
Dave C-J 2014-05-05 08:58:53 +01:00
parent 7674492819
commit bc1fb3b404
3 changed files with 35 additions and 37 deletions

View File

@ -19,7 +19,7 @@ module.exports = function(RED) {
var vm = require("vm");
var fs = require('fs');
var fspath = require('path');
function FunctionNode(n) {
RED.nodes.createNode(this,n);
this.name = n.name;
@ -30,42 +30,42 @@ module.exports = function(RED) {
try {
this.script = vm.createScript(functionText);
this.on("input", function(msg) {
if (msg != null) {
var sandbox = {msg:msg,console:console,util:util,Buffer:Buffer,context:this.context};
try {
this.script.runInNewContext(sandbox);
var results = sandbox.results;
if (results == null) {
results = [];
} else if (results.length == null) {
results = [results];
}
if (msg._topic) {
for (var m in results) {
if (results[m]) {
if (util.isArray(results[m])) {
for (var n in results[m]) {
results[m][n]._topic = msg._topic;
}
} else {
results[m]._topic = msg._topic;
if (msg != null) {
var sandbox = {msg:msg,console:console,util:util,Buffer:Buffer,context:this.context};
try {
this.script.runInNewContext(sandbox);
var results = sandbox.results;
if (results == null) {
results = [];
} else if (results.length == null) {
results = [results];
}
if (msg._topic) {
for (var m in results) {
if (results[m]) {
if (util.isArray(results[m])) {
for (var n in results[m]) {
results[m][n]._topic = msg._topic;
}
} else {
results[m]._topic = msg._topic;
}
}
}
this.send(results);
} catch(err) {
this.error(err);
}
this.send(results);
} catch(err) {
this.error(err);
}
}
});
} catch(err) {
this.error(err);
}
}
RED.nodes.registerType("function",FunctionNode);
RED.library.register("functions");
}

View File

@ -18,24 +18,23 @@ module.exports = function(RED) {
var mustache = require("mustache");
var util = require("util");
var fs = require('fs');
function TemplateNode(n) {
RED.nodes.createNode(this,n);
this.name = n.name;
this.template = n.template;
this.on("input", function(msg) {
if (msg != null) {
try {
msg.payload = mustache.render(this.template,msg)
this.send(msg);
} catch(err) {
this.error(err.message);
}
if (msg != null) {
try {
msg.payload = mustache.render(this.template,msg)
this.send(msg);
} catch(err) {
this.error(err.message);
}
}
});
}
RED.nodes.registerType("template",TemplateNode);
RED.library.register("templates");
}

View File

@ -18,6 +18,5 @@ module.exports = function(RED) {
function CommentNode(n) {
RED.nodes.createNode(this,n);
}
RED.nodes.registerType("comment",CommentNode);
}