简体   繁体   English

如何注入sintra / haml?

[英]how to inject into sintra/haml?

I coded small web app that runs ant (batch file). 我编写了运行ant(批处理文件)的小型web应用程序。 The processing time of the batch file could take up to minutes. 批处理文件的处理时间可能需要几分钟。

index.haml list all available ant files and run.haml runs them. index.haml列出所有可用的ant文件,run.haml运行它们。 The flow how it works now is that when I click a link from index.haml the ant script is run and after it finishes the whole run.haml page is sent to the browser. 它现在的工作流程是,当我点击index.haml中的链接时,运行ant脚本,并在完成整个run.haml页面后发送到浏览器。 So after clicking link from index.haml I still can see index.haml and nothing from run.haml 所以在点击index.haml的链接后,我仍然可以看到index.haml,而不是run.haml

After I click a link from index.haml I want to 点击我想要的index.haml链接后

  • show what script is going to be run and then 显示将要运行的脚本然后
  • run the ant script and then 然后运行ant脚本
  • display the results of it. 显示它的结果。

I was recommended in my other question to use 在我的其他问题中我被推荐使用

I didn't understand how a separate worker thread could help me. 我不明白一个单独的工作线程如何帮助我。 Would the delayed job's results that are captured by ruby's call be sent to the browser once the job finishes? 一旦作业完成,ruby呼叫捕获的延迟作业结果是否会被发送到浏览器?

I also didn't get how I can use Ajax in sinatra. 我也没有得到如何在sinatra中使用Ajax。

Could somebody point me out what a solution for that could be like? 有人可以指出我的解决方案是什么样的? Please note that I know bit of ruby, learned bit of sinatra and haml yesterday. 请注意,我知道一点红宝石,昨天学习了一些sinatra和haml。 No nothing about Ajax :-) I learn by examples ... and happy to learn anything. 没有关于Ajax的事情:-)我通过实例学习......并乐于学习任何东西。

index.haml gives me html like index.haml给我html之类的

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
      <head>
        <title>Available test suits</title>
      </head>
      <body>
        <h1>Available test suits</h1>
        <br/><br/>
        <a href='run?run=login_build'>login_build</a>
        <br />
        <a href='run?run=login_cycle_build'>login_cycle_build</a>
        <br />
        <a href='run?run=login_cycle_academicyear_build'>login_cycle_academicyear_build</a>
      </body>
    </html>

run.haml looks like run.haml看起来像

    !!!  
    %html  
      %head  
        %title Running  
      %body
        = "Starting test suite: #{params['run']}"
        - output = %x[cd C:\\Program Files\\TestPro\\TestPro Automation Framework410 && ant -lib lib -f "C:\\Program Files\\TestPro\\TestPro Automation Framework410\\Output Files\\builds\\#{params['run']}.xml"]
        -#The result is
        %br
        = output.split("\n")[-2,2].join("<BR>")
        = "<br/>"*2
        %a(href="/")back to suits list

If you want to use Ajax must, choose a javascript library Dojo, Prototype, Mootools, jQuery... Each of them has specific tools for handling ajax requests. 如果你想使用Ajax必须,选择一个javascript库Dojo,Prototype,Mootools,jQuery ......他们每个人都有处理ajax请求的特定工具。

In javascript you make an xhr(Ajax request), which may be asynchronous and still Sinatra serves you the wanted content, you can display in webpage what you want. 在javascript中你创建了一个xhr(Ajax请求),它可能是异步的,而Sinatra仍然为你提供想要的内容,你可以在网页上显示你想要的内容。

var xhrLoadClientMenu = function(param){
var result_node = dojo.byId('div_menu')
var xhrArgs = {
  // the Sinatra get "/run" ... end handler
  url: '/run',
  load: function(data){
    // When response is rendered come's here 
    result_node.innerHTML = data;
  },
  error: function(error){
    msg = "<p>Ooops some error ...<br><br>" + error + "</p>";
   }
 }
  // Here you can put what you want to display durring loading
  result_node.innerHtml = "Loading..." 
  var defered = dojo.xhrGet(xhrArgs)
}

a xhrGet example using dojo. 使用dojo的xhrGet示例。 See inline comments. 请参阅内联评论。

  • xhrLoadClientMenu(param) is triggered by an event; xhrLoadClientMenu(param)由事件触发;
  • a HTML element with id #div_menu from page is stored in result_node object; 来自页面的id为#div_menu的HTML元素存储在result_node对象中;
  • xhrArgs object sets the Ajax request properties like: xhrArgs对象设置Ajax请求属性,如:
    1. url: the Sinatra handler which renders the content; url:呈现内容的Sinatra处理程序;
    2. load: function gets the response from sinatra and replaces result_node content when load is completed load:function从sinatra获取响应并在load完成时替换result_node内容
    3. error if something goes wrong this message will be shown in result_node; 错误,如果出现问题,此消息将显示在result_node中;
  • meanwhile you may set the result_node content to anything while the real content is loading; 同时你可以在加载真实内容时将result_node内容设置为任何内容;
  • last line executes the xhr request. 最后一行执行xhr请求。

All of this is happening without a page reload. 所有这一切都在没有页面重新加载的情况下发生。

Delayed_job results are indeed sent "back to the browser." Delayed_job结果确实已“返回浏览器”。 Please take another look at this live example of using Sinatra and DJ to process a task asynchronously and push the result to the browser once the task is complete . 再看一下这个使用Sinatra和DJ异步处理任务的实例,并在任务完成后将结果推送到浏览器

The call runs asynchronously, and the rest of the page loads, and when the calls are complete the data is posted back to the page. 该调用以异步方式运行,并且页面的其余部分加载,并且当调用完成时,数据将发布回页面。 Now, since it's on the cloud, it may seem VERY delayed; 现在,因为它在云端,它可能看起来非常延迟; you'll want to download the source and run it on your own machine to see what kind of speed DJ offers on a local network. 您需要下载源代码并在自己的计算机上运行,​​以查看DJ在本地网络上提供的速度。 The results are indeed processed and sent back; 确实处理并发回结果; scroll down the page and you'll see the results of earlier processing -- you can even enter your own text block to add to the processing queue. 向下滚动页面,您将看到早期处理的结果 - 您甚至可以输入自己的文本块以添加到处理队列中。

So perhaps you could utilize delayed_job to create a queue for asynchronous processing of your test suites -- 'out of sync' from the rest of your page, but also with the option of passing data back 'incrementally' as well as once the task completes. 因此,也许您可​​以利用delayed_job创建一个队列,用于异步处理您的测试套件 - 从页面的其余部分“不同步”,还可以选择将数据“递增地”传回,以及任务完成后。

Now, Ajax offers another way to provide this same functionality. 现在,Ajax提供了另一种提供相同功能的方法。 I absolutely and unequivocally recommend getting one of the Javascript frameworks and learning how to do this sort of asynchronous processing that way as well. 我绝对明确地建议获得一个Javascript框架,并学习如何以这种方式进行这种异步处理。 However, based on your earlier question, I would still suggest working through the source code of the example above using Sinatra and delayed_job , as it's already extremely close to what you are trying to do, not to mention that it keeps you entirely in Ruby. 但是,基于你之前的问题,我仍然建议使用Sinatra和delayed_job来完成上面示例的源代码 ,因为它已经非常接近你想要做的事情,更不用说它让你完全使用Ruby了。

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

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