mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
load context data only when using
This commit is contained in:
parent
37007f54fb
commit
843afd075f
@ -252,40 +252,42 @@
|
||||
|
||||
// convert context data base on format
|
||||
function convertContextData(data) {
|
||||
const context = data[RED.settings.context.default]
|
||||
Object.keys(context).forEach((key) => {
|
||||
switch (context[key].format) {
|
||||
case "Object":
|
||||
context[key] = JSON.parse(context[key].msg);
|
||||
break;
|
||||
case "number":
|
||||
context[key] = Number(context[key].msg);
|
||||
break;
|
||||
case "boolean":
|
||||
context[key] = context[key].msg === 'true';
|
||||
break;
|
||||
default:
|
||||
// format string[*] and unknown format will go here
|
||||
context[key] = context[key].msg;
|
||||
break;
|
||||
}
|
||||
})
|
||||
let context = data;
|
||||
switch (context.format) {
|
||||
case "Object":
|
||||
context = JSON.parse(context.msg);
|
||||
break;
|
||||
case "number":
|
||||
context = Number(context.msg);
|
||||
break;
|
||||
case "boolean":
|
||||
context = context.msg === 'true';
|
||||
break;
|
||||
case "undefined":
|
||||
context = undefined;
|
||||
break;
|
||||
default:
|
||||
// format string[*] and unknown format will go here
|
||||
context = context.msg;
|
||||
break;
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
let flowContext;
|
||||
const flowId = RED.workspaces.active();
|
||||
$.getJSON("context/flow/" + flowId,function (data) {
|
||||
flowContext = convertContextData(data);
|
||||
// trigger testExpression after context data fetched
|
||||
expressionEditor.setValue(expressionEditor.getValue(),-1);
|
||||
})
|
||||
let globalContext;
|
||||
$.getJSON("context/global",function (data) {
|
||||
globalContext = convertContextData(data);
|
||||
// trigger testExpression after context data fetched
|
||||
expressionEditor.setValue(expressionEditor.getValue(),-1);
|
||||
})
|
||||
let flowContext={};
|
||||
var fetchFlowContext = function(key){
|
||||
$.getJSON("context/flow/" + RED.workspaces.active()+"/"+key,function (data) {
|
||||
flowContext[key] = convertContextData(data);
|
||||
testExpression();
|
||||
})
|
||||
};
|
||||
let globalContext={};
|
||||
var fetchGlobalContext = function(key){
|
||||
$.getJSON("context/global/"+key,function (data) {
|
||||
globalContext[key] = convertContextData(data);
|
||||
testExpression();
|
||||
})
|
||||
};
|
||||
var testExpression = function() {
|
||||
var value = testDataEditor.getValue();
|
||||
var parsedData;
|
||||
@ -306,16 +308,22 @@
|
||||
}
|
||||
}
|
||||
expr.assign('flowContext',function(val){
|
||||
if (flowContext) {
|
||||
return getValueFromContext(val, flowContext);
|
||||
var key = val.split('.')[0];
|
||||
if (flowContext.hasOwnProperty(key)) {
|
||||
return getValueFromContext(val,flowContext);
|
||||
}else if (key){
|
||||
fetchFlowContext(key);
|
||||
}
|
||||
return null;
|
||||
return undefined;
|
||||
})
|
||||
expr.assign('globalContext',function(val){
|
||||
if (globalContext) {
|
||||
return getValueFromContext(val, globalContext);
|
||||
var key = val.split('.')[0];
|
||||
if (globalContext.hasOwnProperty(key)) {
|
||||
return getValueFromContext(val,globalContext);
|
||||
}else if (key){
|
||||
fetchGlobalContext(key);
|
||||
}
|
||||
return null;
|
||||
return undefined;
|
||||
});
|
||||
expr.assign("env", function(name) {
|
||||
usesEnv = true;
|
||||
|
Loading…
Reference in New Issue
Block a user