简体   繁体   English

让服务器返回一个JavaScript函数然后被调用的干净方法是什么?

[英]What's a clean way to have the server return a JavaScript function which would then be invoked?

My application is architected as a host of plug-ins that have not yet been written. 我的应用程序被设计为一系列尚未编写的插件。 There's a long reason for this, but with each new year, the business logic will be different and we don't know what it will be like (Think of TurboTax if that helps). 这有很长一段时间的原因,但每次新的一年,业务逻辑都会有所不同,我们不知道它会是什么样的(想想TurboTax,如果有帮助的话)。 The plug-ins consist of both server and client components. 插件由服务器和客户端组件组成。 The server components deals with business logic and persisting the data into database tables which will be created at a later time as well. 服务器组件处理业务逻辑并将数据持久保存到数据库表中,数据库表也将在以后创建。 The JavaScript manipulates the DOM for the browsers to render afterward. JavaScript操纵DOM以供浏览器随后呈现。

Each plugin lives in a separate assembly, so that they won't disturb the main application, ie, we don't want to recompile the main application. 每个插件都存在于一个单独的程序集中,因此它们不会干扰主应用程序,即我们不想重新编译主应用程序。 Long story short, I am looking for a way to return JavaScript functions to the client from an Ajax get request, and execute these JavaScript functions (which are just returned). 简而言之,我正在寻找一种从Ajax get请求返回JavaScript函数到客户端的方法,并执行这些JavaScript函数(刚刚返回)。 Invoking a function in Javascript is easy. 在Javascript中调用函数很容易。 The hard part is how to organize or structure so that I won't have to deal with maintenance problem. 困难的部分是如何组织或结构,以便我不必处理维护问题。 So concat using StringBuilder to end up with JavaScript code as a result of calling toString() from the string builder object is out of the question. 因此,使用StringBuilder结束使用JavaScript代码作为从字符串构建器对象调用toString()的结果是不可能的。

I want to have no difference between writing JavaScript codes normally and writing Javascript codes for this dynamic purpose. 我希望在正常编写JavaScript代码和为此动态目的编写Javascript代码之间没有区别。

An alternative is to manipulate the DOM on the server side, but I doubt that it would be as elegantly as using jQuery on the client side. 另一种方法是在服务器端操作DOM,但我怀疑它在客户端使用jQuery会有多优雅。 I am open for a C# library that supports chainable calls like jQuery that also manipulates the DOM too. 我对C#库开放,它支持像jQuery这样的可链接调用,它也可以操作DOM。

Do you have any idea or is it too much to ask or are you too confused? 你有什么想法,或者问得太多,或者你太困惑了?

Edit1 : The point is to avoid recompiling, hence the plug-ins architecture. Edit1 :重点是避免重新编译,因此插件架构。 In some other parts of the program, I already use the concept of dynamically loading Javascript files. 在程序的其他一些部分,我已经使用了动态加载Javascript文件的概念。 That works fine. 这很好。 What I am looking here is somewhere in the middle of the program when an Ajax request is sent to the server. 当我向服务器发送Ajax请求时,我在这里看到的是程序中间的某个地方。

Edit 2 : To illustrate my question: 编辑2 :说明我的问题:

Normally, you would see the following code. 通常,您会看到以下代码。 An Ajax request is sent to the server, a JSON result is returned to the client which then uses jQuery to manipulate the DOM (creating tag and adding to the container in this case). 将Ajax请求发送到服务器,将JSON结果返回给客户端,然后客户端使用jQuery来操作DOM(在这种情况下创建标记并添加到容器中)。

var container = $('#some-existing-element-on-the-page');
$.ajax({
    type: 'get',
    url: someUrl,
    data: {'': ''},
    success: function(data) {                                                   
      var ul = $('<ul>').appendTo(container);

      var decoded = $.parseJSON(data);                
      $.each(decoded, function(i, e) {
         var li = $('<li>').text(e.FIELD1 + ',' + e.FIELD2 + ',' + e.FIELD3)
            .appendTo(ul);
      });                
    }        
});

The above is extremely simple. 以上非常简单。 But next year, what the server returns is totally different and how the data to be rendered would also be different. 但是明年,服务器返回的内容完全不同,呈现的数据也会有所不同。 In a way, this is what I want: 在某种程度上,这就是我想要的:

var container = $('#some-existing-element-on-the-page');
$.ajax({
    type: 'get',
    url: someUrl,
    data: {'': ''},
    success: function(data) {                           
      var decoded = $.parseJSON(data); 
      var fx = decoded.fx;
      var data = decode.data;
      //fx is the dynamic function that create the DOM from the data and append to the existing container
      fx(container, data);                  
    }        
});

I don't need to know, at this time what data would be like, but in the future I will, and I can then write fx accordingly. 我不需要知道,此时会有什么数据,但将来我会,然后我可以相应地编写fx。

I doubt you would need the ajax to return the javascript and you could probably just change the .js files each year. 我怀疑你需要ajax来返回javascript,你可能只是每年更改.js文件。

Note that your scripts will need to be signed if they run cross domain 请注意,如果您的脚本跨域运行,则需要对其进行签名

Anyway, you're probably looking for EVAL, which could in theory be used by hackers to mess with your site. 无论如何,你可能正在寻找EVAL,理论上黑客可以利用它来搞乱你的网站。 It would also slow things down, and make things harder to debug... 它也会减慢速度,使调试变得更难......

Again, you probably shouldn't need to load javascript asynchronously. 同样,您可能不需要异步加载JavaScript。 If the logic changes every year, that's just changing .js files. 如果逻辑每年都在变化,那就是改变.js文件。 You would need to load it with ajax if the logic would change while the browser was still on the page... which could cause a problem in the programming namespaces anyway. 如果逻辑在浏览器仍在页面上时发生变化,则需要使用ajax加载它...这可能会导致编程命名空间出现问题。 (ie, you introduce new code in the live namespace of the old code, and the versions don't work nicely). (即,您在旧代码的实时命名空间中引入了新代码,并且版本不能很好地工作)。

愚蠢的想法,但我可以建议您返回一个href到外部JS文件并加载而不是返回实际的JS代码?

It sounds like you have already decided on your architecture and you are determined to find a way to make it work. 听起来你已经决定了你的架构,你决心找到一种方法让它工作。 But rather than modify the DOM directly, why not just have the plug-ins return the relevant HTML and update the innerHTML of the container with their output? 但是,为什么不直接修改DOM,为什么不让插件返回相关的HTML并用它们的输出更新容器的innerHTML?

Well, the cop-out is simply using eval. 好吧,警察只是使用eval。 Sending the function as string from the server and eval('myhopefullyuncompromisedjavascriptcode'); 从服务器和eval('myhopefullyuncompromisedjavascriptcode')发送函数作为字符串;

success: function(data) {  
var decoded = $.parseJSON(data); 
  var fx = eval(decoded.fx); 
  //where the eval string would contain "(function(params){...})"
  var data = decode.data;
  //fx is the dynamic function that creates the DOM from the data and append to the existing container
  fx(container, data);      

The alternatives are not really that much greater though. 但是,替代方案并没有那么大。

You could load on demand but, really, it amounts to the same thing except that it is encapsulated in a .js. 您可以按需加载,但实际上,除了它被封装在.js中之外,它实际上是相同的。
If someone has the ability to return random stringified js from your site, or, is able to make your ajax call point at somewhere else to load arbitrary js, then they are already able to load their own arbitrary js. 如果有人能够从您的站点返回随机字符串化的js,或者能够在其他地方使用ajax调用点来加载任意js,那么他们已经能够加载他们自己的任意js。

On the other hand, dynamic loading could save you some bw, in that you aren't sending the same stringified function over the wire and evaling it time and again, that same benefit could be derived from maintaining a clientside hash of functions though. 另一方面,动态加载可以为您节省一些bw,因为您不是通过线路发送相同的字符串化函数并且一次又一次地评估它,但是可以通过维护函数的客户端哈希来获得相同的好处。

its a bit 'meh' from my pov. 我的pov有点'meh'。

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

相关问题 在JavaScript中共享异步函数响应的干净方法是什么? - What's a clean way to share async function response in Javascript? 在 Javascript 中级联函数的干净方法是什么? - What's the clean way to cascade functions in Javascript? 在新调用的函数中,`new Function(“return this”)()`的目的是什么? - what's the purpose of `new Function(“return this”)()` in immediately invoked function javascript:干净的方式让return语句跨越多行 - javascript: clean way to have a return statement span multiple lines 使用JavaScript或JQuery生成和附加HTML的干净有效方法是什么? - What's a clean and efficient way to generate and append HTML with JavaScript or JQuery? 如果清除这些语句,最好的方法是什么? - What would the best way to clean these if statements up? JavaScript在构造函数和作为构造函数调用的函数返回对象之间有什么区别? - What difference is there in JavaScript between a constructor function, and function returning object which is invoked as a constructor? 调用JavaScript函数的正确方法是什么? - What's the correct way to call JavaScript Function? 销毁javascript函数的正确方法是什么? - what's the correct way to destroy a javascript function? 调用 javascript function 的最快方法是什么 - What's the fastest way to call a javascript function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM