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

Ensure custom mustache context parent set in Template node

fixes #1126
This commit is contained in:
Nick O'Leary 2017-01-23 15:34:34 +00:00
parent fd6f7cd881
commit 7759aacb35
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 40 additions and 20 deletions

View File

@ -22,8 +22,8 @@ module.exports = function(RED) {
* Custom Mustache Context capable to resolve message property and node * Custom Mustache Context capable to resolve message property and node
* flow and global context * flow and global context
*/ */
function NodeContext(msg, nodeContext) { function NodeContext(msg, nodeContext,parent) {
this.msgContext = new mustache.Context(msg); this.msgContext = new mustache.Context(msg,parent);
this.nodeContext = nodeContext; this.nodeContext = nodeContext;
} }
@ -31,26 +31,34 @@ module.exports = function(RED) {
NodeContext.prototype.lookup = function (name) { NodeContext.prototype.lookup = function (name) {
// try message first: // try message first:
var value = this.msgContext.lookup(name); try {
if (value !== undefined) { var value = this.msgContext.lookup(name);
return value; if (value !== undefined) {
} return value;
// try node context:
var dot = name.indexOf(".");
if (dot > 0) {
var contextName = name.substr(0, dot);
var variableName = name.substr(dot + 1);
if (contextName === "flow" && this.nodeContext.flow) {
return this.nodeContext.flow.get(variableName);
} }
else if (contextName === "global" && this.nodeContext.global) {
return this.nodeContext.global.get(variableName); // try node context:
var dot = name.indexOf(".");
if (dot > 0) {
var contextName = name.substr(0, dot);
var variableName = name.substr(dot + 1);
if (contextName === "flow" && this.nodeContext.flow) {
return this.nodeContext.flow.get(variableName);
}
else if (contextName === "global" && this.nodeContext.global) {
return this.nodeContext.global.get(variableName);
}
} }
}catch(err) {
throw err;
} }
} }
NodeContext.prototype.push = function push (view) {
return new NodeContext(view, this.nodeContext,this.msgContext);
};
function TemplateNode(n) { function TemplateNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.name = n.name; this.name = n.name;
@ -64,7 +72,7 @@ module.exports = function(RED) {
try { try {
var value; var value;
if (node.syntax === "mustache") { if (node.syntax === "mustache") {
value = mustache.render(node.template, new NodeContext(msg, node.context())); value = mustache.render(node.template,new NodeContext(msg, node.context()));
} else { } else {
value = node.template; value = node.template;
} }

View File

@ -166,6 +166,18 @@ describe('template node', function() {
}); });
}); });
it('should handle block contexts objects', function(done) {
var flow = [{id:"n1", type:"template", template: "A{{#payload.A}}{{payload.A}}{{.}}{{/payload.A}}B",wires:[["n2"]]},{id:"n2",type:"helper"}];
helper.load(templateNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property('payload','AabcabcB');
done();
});
n1.receive({payload:{A:"abc"}});
});
});
it('should raise error if passed bad template', function(done) { it('should raise error if passed bad template', function(done) {
var flow = [{id:"n1", type:"template", field: "payload", template: "payload={{payload",wires:[["n2"]]},{id:"n2",type:"helper"}]; var flow = [{id:"n1", type:"template", field: "payload", template: "payload={{payload",wires:[["n2"]]},{id:"n2",type:"helper"}];
helper.load(templateNode, flow, function() { helper.load(templateNode, flow, function() {