将 Rails 与 Comet 服务器集成的设计模式
我有一个 Ruby on Rails (2.3.5) 应用程序和一个 APE(Ajax 推送引擎)服务器。当在 Rails 应用程序中创建记录时,我需要将新记录通过适用的通道推送到 APE 服务器。记录可以通过控制器的创建操作通过传统路径在 Rails 应用程序中创建,也可以由多个事件机创建,这些事件机不断监视各种输入流并在看到满足特定条件的数据时创建记录。
在我看来,将数据推送到 APE 服务器(进而将数据推送到客户端)的代码的最佳/正确位置是在模型的 after_create 挂钩中(因为并非所有记录创建都会流经控制器的创建操作)。
最后需要注意的是,我想将一段格式化的 HTML 推送到 APE 服务器(而不是数据的 JSON 表示形式)。我想这样做的原因是 1) 我已经有逻辑在现有的部分中生成所需的布局 2) 我不想创建部分的 javascript 实现(javascript 接受 JSON 对象并创建周围的所有 HTML用于演示)。这很快就会成为维护的噩梦。 这样做的问题是,它需要从模型中“渲染”部分(无论如何,我都很难做到这一点,因为当它们以这种方式渲染时,它们似乎无法访问助手)。
不管怎样——只是想知道组织这一切的正确方法是什么。
谢谢
I have a Ruby on Rails (2.3.5) application and an APE (Ajax Push Engine) server. When records are created within the Rails application, i need to push the new record out on applicable channels to the APE server. Records can be created in the rails app by the traditional path through the controller's create action, or it can be created by several event machines that are constantly monitoring various inputstream and creating records when they see data that meets a certain criteria.
It seems to me that the best/right place to put the code that pushes the data out to the APE server (which in turn pushes it out to the clients) is in the Model's after_create hook (since not all record creations will flow through the controller's create action).
The final caveat is I want to push a piece of formatted HTML out to the APE server (rather than a JSON representation of the data). The reason I want to do this is 1) I already have logic to produce the desired layout in existing partials 2) I don't want to create a javascript implementation of the partials (javascript that takes a JSON object and creates all the HTML around it for presentation). This would quickly become a maintenance nightmare.
The problem with this is it would require "rendering" partials from within the Model (which im having trouble doing anyhow because they don't seem to have access to Helpers when they're rendered in this manner).
Anyhow - Just wondering what the right way to go about organizing all of this is.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在与 #rails 和 #ape 中的一些人交谈后,这似乎是解决此问题的最佳方法。
After talking w some folks in #rails and #ape this is appears to be the best approach to this issue.