本文介绍了EmberJS和WebSocket |最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我不能为自己正确回答的问题,也许是因为我缺乏对EmberJS的支持。



我必须开发一个管理界面在EmberJS中,使用Symfony 2作为后台,它应该像桌面应用程序一样行动起来。到目前为止这么好,但由于很多人会使用这个应用程序中的数据,我真的很想使用一个WebSocket适配器实现EmberJS,因为每个连接的客户端应该始终知道实体的变化立即(或asap)。我可以为EmberJS编写一个WebSocket适配器,但是我的问题是WebSocket会做更多的RESTful操作,而且服务器将发送没有任何EmberJS请求的消息(例如,一个实体已更改,并且服务器将此更改广播到所有客户端)。这意味着在RESTful操作之上,我需要一个命令结构,就我的expierience而言,它不会与纯DS适配器一起使用。



例如:



也许我会触发一个控制器方法,发送一个这样的websocket消息:



{command:say,参数:{message:Hello guys!}}



此命令不是Entity DS)相关,永远不会进入应用程序商店。



另一个例子就是这样:



code> {command:load entity,参数:{type:Vendor\Bundle\Entity\Type,id:43}}



哪个将加载应该存储在应用程序存储中的实体。



那么,正如我所说,我不太熟悉EmberJS我可以弄清楚最好的方法是什么。我应该完全绕过DS适配器,检查isDirty和加载实体之后的push方法吗?我很高兴你有任何想法!

解决方案

据我了解您的问题,您想将更改从后台推送到单页应用程序?



您可以使用 self.store.push('modelName',json)将自定义JSON推送到您的应用程序的Ember中的商店中 。查看,以获得更好的不起眼。 / p>

所以例如,如果你的服务器通过websocket发送你的这样看起来像这样

  {
- message:{
type:fooModel,
data:{
... //这里的模型属性
}
}
}

您可以将数据推入您的商店。以下代码段可以与SocketIO一起工作,例如:

  App.ApplicationRoute = Ember.Route.extend({
activate :function(){
//一旦我们进入应用程序路由,连接到websocket
var socket = window.io.connect('http:// localhost:8080');

var self = this;

socket.on('message',function(data){
self.store.push(data.type,data.item);
});
}
});

您可以轻松地修改此代码段以满足您的需求。


I have a again which I can't answer for my self properly, maybe because of my lack in expierience with EmberJS.

I have to develop a management interface in EmberJS, using Symfony 2 for the backend, which should act and feel like a desktop application. So far so good, but since alot of people will work with the data inside this application, i would really like to use a WebSocket adapter implementation for EmberJS, since every connected client should always know about changes in entities immediately (or asap). I could write a WebSocket adapter for EmberJS but my problem here is that the WebSocket will do much more then RESTful operations, also the server will send messages without any EmberJS request (e.g. an entity changed and the server broadcasting this change to all clients). That means that i need a "command" structure on top of RESTful operations which, as far as my expierience goes, will not work with a pure DS Adapter.

For example:

Maybe i will trigger a controller method that will send a websocket message like this:

{command: "say", parameters: {message: "Hello guys!"} }

This command is not Entity (DS) related and will never go into the application store.

Another example would be like this:

{command: "load entity", parameters: {type: "Vendor\Bundle\Entity\Type", id: 43} }

Which would load an entity which should be stored in the application store.

Well, as i said, im not that familiar with EmberJS that I could figure out which the best approach could be. Should I bypass the DS Adapter completely and check for "isDirty" and just the push methods after loading entities? I'm happy about any idea you have!

解决方案

As far as I understand your question, you want to push changes from your backend to your single page app?

You can push custom JSON into your application's store in Ember by using self.store.push('modelName', json). Have a look at the docs for a better undestanding.

So for example if your server sends you JSON via websocket that looks like this

{ 
  - "message": {
    "type": "fooModel", 
    "data": {
      ... // Model attributes here 
    }
  }
}

you can push the data into your store. The following snippet would work with SocketIO for example:

App.ApplicationRoute = Ember.Route.extend({                                                              
  activate: function() {                
    // connect to the websocket once we enter the application route                                                                   
    var socket = window.io.connect('http://localhost:8080');                                                           

    var self = this;                                                                                                   

    socket.on('message', function(data){                                                                               
      self.store.push(data.type, data.item);                                                                              
    });                                                                                                                
  }
});  

You can easily modify this snippet to fit your needs.

这篇关于EmberJS和WebSocket |最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 04:57