简体   繁体   English

节点 JS - 将 function 本身发送到客户端,function 在客户端(不在服务器中)运行

[英]node JS - Send function itself to the client, that the function run in client (not in server)

I am building a system in nodeJS that the user has to go through lots of forms and after each form the user fills, he sends the form's DATA to the nodeJS server and receives another form according to the data he filled in the previous form.我正在 nodeJS 中构建一个系统,用户必须通过大量 forms 来 go 并且在用户填写每个表单后,他将表单的数据发送到 nodeJS 服务器并根据他在前一个表单中填写的数据接收另一个表单。

I have defined in JS files objects that contain all forms.我在 JS 文件中定义了包含所有 forms 的对象。 Then each time the user receives JS object that contains the settings of the form.然后每次用户收到包含表单设置的 JS object。

And there is a code in the client that knows how to create a form by js object.并且客户端中有一段代码知道如何通过js object创建表单。 The problem is, some of the data is dynamic according to the behavior of the client.问题是,根据客户端的行为,一些数据是动态的。

And I want to send to the client a function that will run on the client (and affect the form).我想向客户端发送一个 function 将在客户端上运行(并影响表单)。 Is there an elegant way to send a function to the client (do not run the function in the server).是否有一种优雅的方式将 function 发送到客户端(不要在服务器中运行 function)。

I realized that using EVAL is not recommended at all.我意识到根本不推荐使用 EVAL。 Also to make sure that all the functions are already in the client, And in the settings file to write only the function name makes it very difficult for me.还要确保所有功能都已经在客户端中,并且在设置文件中只写 function 名称对我来说非常困难。 Is there a convenient way to do this?有没有方便的方法来做到这一点?

For example, attaching Setting Field:例如,附加设置字段:

{
    name: 'appleNum',
    type: 'number',
    title: 'How many apples did you eat today',
    Required: true,
    validation: function (value) { 
        if (value >= 0)
            return true; 
    },
    MaxChars: null,
    default: null,
},

I want to send the validation function to the client And to run in the client.我想将验证 function 发送到客户端并在客户端中运行。 thank for all.谢谢大家。

I assume that you actually communicate with the client via JSON.我假设您实际上是通过 JSON 与客户端进行通信的。 JSON and a javascript objects are very different things. JSON 和 javascript 对象是非常不同的东西。 JSON just represents the data of a javascript object. JSON 仅代表 javascript object 的数据。 It can not hold any methods to be executed.它不能保存任何要执行的方法。

However there is a possibility to run code on the client side, that you setup on the server.但是,有可能在客户端运行代码,您在服务器上设置。 This works with so called remote procedure calls .这适用于所谓的远程过程调用 For node there is a library called eureca.io .对于节点,有一个名为eureca.io的库。 Maybe this is of any help to you.也许这对你有任何帮助。

You can convert your function to a string with toString method on server side and you can generate that function from string with a Function constructor on the client side.您可以在服务器端使用toString方法将 function 转换为字符串,并且可以在客户端使用Function 构造函数从字符串生成 function。

Below snippet shows a sample stringification and function generation code下面的代码片段显示了示例字符串化和 function 生成代码

 // lets say a you have a function f function f(value) { if (value >= 0) return true; } // you can convert this function to a string like this var stringified_function = f.toString(); // this string can be safely transferred in a json between client&server console.log(stringified_function); // you can re-create that function from string like this on client side var new_f = new Function("return " + stringified_function)() // now you can use the new function on client side console.log(new_f(0)); // returns true console.log(new_f(3)); // returns true console.log(new_f(-1)); // returns undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM