前端处理业务问题:)

前端处理业务问题

需求想通过后端数据修改前端样式(算法端的不写后台逻辑)
  1. 发来数据
[{
    ChoiceSource: "data://product_param?col=1",
    Choices: [],
    Condition: "",
}]

2.数据解释
ChoiceSource为数据源可以是http://或其他协议开头
Condition为条件判断传"xxx[=(!=)(>=)(<=)]xxx"然后禁用该编辑模块
image

问题

一般这种情况该怎么优化前端(后台数据可能随时变化)
有没有什么简单的脚本语言(js可以简单处理的)来优化Condition条件(感觉会有奇怪的条件需求)

阅读 1.7k
2 个回答

勉强实现

    () => {
        const env_params = 'a, b'
        const env_args = [1, 2]
        const condition = 'a === b'
        // env_params 参数
        const command = `function(${env_params}){ return ${condition}}`;
        return Function(`"use strict";return ${command}`)().apply(null, env_args);
    }

ps: 有没有更好在Function块级作用域生成变量的方法

var scriptElement = document.createElement('script');
scriptElement.setAttribute('id','____tmp_condition_script_____');
scriptElement.innerText="function test(){console.log('~~gogo~~');return true;}";//传入自定义函数
var header = document.querySelector('head');
header.appendChild(scriptElement);

var conditionResult = test();//执行自定义函数
header.removeChild(scriptElement);//删除script元素
test = null;//删除test全局函数
推荐问题