简体   繁体   English

将Ember.js与Sock.js结合使用

[英]Using Ember.js with Sock.js

Is there any tutorial on how to use Sock.js with Ember.js? 是否有关于如何将Sock.js与Ember.js结合使用的教程? I can't find any. 我找不到。 Any example, tutorial or guide would be nice. 任何示例,教程或指南都不错。 I can use sock.js in plain js like: 我可以在普通js中使用sock.js,例如:

var sock = new SockJS('/echo');
sock.onmessage = function(e){
    console.log(e.data);
}
sendButton.onclick = function() {
    var text = field.value;
    sock.send(text);
}

But don't know how to use it with Ember.js 但是不知道如何在Ember.js中使用它

In Ember you'd probably want to have a high level controller/route that sets up the sock. 在Ember中,您可能想要一个高级控制器/路由来设置袜子。 Then when a message comes in, assuming that the data property of the message is the JSON representation of a model you can use push on the store . 然后,当收到一条消息时,假设消息的data属性是模型的JSON表示形式,则可以在store上使用push Something like this should do it: 这样的事情应该做到:

App.Message = DS.Model.extend({...});

App.ApplicationRoute = Ember.Route.extend({
  setupController : function(controller, model){
    var sock = new SockJS('/echo');
    var store = this.get('store');
    sock.onmessage = function(e){
      store.push('message',e.data);
    }
  }
});

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

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