简体   繁体   English

Ember JS中的服务器发送事件

[英]Server Sent Events in Ember JS

I have an App in which on clicking a link the browser needs to trigger something on server and client is supposed to get progress of it. 我有一个应用程序,在点击链接浏览器需要触发服务器上的东西,客户端应该得到它的进展。 SSE is a good candidate for this but I am unable to figure this out in ember (which I am learning). SSE是一个很好的候选人,但我无法在余烬中解决这个问题(我正在学习)。

I tried with the template execute : 我尝试使用模板execute

<script type="text/x-handlebars" data-template-name="execute">
    <script>
       if(typeof(EventSource)!=="undefined") {
          var source=new EventSource("/execute/flow");
          source.onmessage=function(event) {
              if(event.data == 'end') {
                source.close();
              }else{
                document.getElementById("flow_result").innerHTML+= event.data + "<br>";
              }
          };
       } else {
          document.getElementById("flow_result").innerHTML="Sorry, your browser does not support server-sent events...";
       }
    }
    <script>
<script>

Somewhere on the page there is a link using {{#link-to 'execute'}} which triggers this template. 在页面的某个位置有一个链接使用{{#link-to 'execute'}}来触发此模板。 But somehow this SSE javascript is not executed at all. 但不知何故,这个SSE javascript根本没有执行。

Questions: 问题:

1) What I am I doing wrong? 1)我做错了什么? (I suspect that I should not put javascript code in template) 2) Is there any good example of using SSE with ember js? (我怀疑我不应该把javascript代码放在模板中)2)有没有使用SSE和ember js的好例子?

Don't pass code in the template. 不要在模板中传递代码。 Handle whatever actions you like in the controllers and routes of your application. 在控制器和应用程序的路径中处理您喜欢的任何操作。 For sse you can do the following: 对于sse,您可以执行以下操作:

App.ApplicationController = Ember.Controller.extend({
  init: function() {
    var eventSource;
    eventSource = new EventSource(your-event-source);
    return eventSource.addEventListener(your-event-to-listen, function(e) {
      var data;
      return data = $.parseJSON(e.data);
      //Do whatever else you want in here with your data
    });
  }
});

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

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