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
* flow and global context
*/
function NodeContext(msg, nodeContext) {
this.msgContext = new mustache.Context(msg);
function NodeContext(msg, nodeContext,parent) {
this.msgContext = new mustache.Context(msg,parent);
this.nodeContext = nodeContext;
}
@ -31,6 +31,7 @@ module.exports = function(RED) {
NodeContext.prototype.lookup = function (name) {
// try message first:
try {
var value = this.msgContext.lookup(name);
if (value !== undefined) {
return value;
@ -49,7 +50,14 @@ module.exports = function(RED) {
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) {
RED.nodes.createNode(this,n);
@ -64,7 +72,7 @@ module.exports = function(RED) {
try {
var value;
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 {
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) {
var flow = [{id:"n1", type:"template", field: "payload", template: "payload={{payload",wires:[["n2"]]},{id:"n2",type:"helper"}];
helper.load(templateNode, flow, function() {