在 Pusher 事件的回调函数中渲染 Rails 部分。
我正在开发一个相当简单的 Rails 应用程序,人们可以在其中提问和投票。我想构建一些实时功能,并决定使用 Pusher 来实现这一点(主要是因为它看起来与在 heroku 上部署非常兼容)。
目前,当您添加新问题时,将执行以下 javascript 代码:
if $('.question').hasClass('no_current_questions_notice')
$('.no_current_questions_notice').fadeOut 400, ->
$(this).replaceWith('<%= escape_javascript(render 'questions/added_question', :question => @added_question) %>')
$('.last_question').hide().fadeIn 400
else
$('.last_question').removeClass('last_question')
$('#added_question').replaceWith('<%= escape_javascript(render 'questions/added_question', :question => @added_question) %>')
$('.last_question').hide().fadeIn 400
我希望可以简单地在 Pusher 回调中重用我的部分内容,这样我就可以重新创建 Pusher 事件的 AJAX 请求之后发生的情况。请参阅下面最简单的形式:
<script type='text/javascript'>
var pusher = new Pusher('<%= Pusher.key %>');
var channel = pusher.subscribe('course-<%= @course.id %>');
channel.bind('create_question', function(data) {
var @added_question = data;
$('#added_question').replaceWith('<%= escape_javascript(render 'questions/added_question', :question => @added_question) %>');
});
</script>
作为参考,这是我的部分内容:
<div class='question last_question'>
<div class='question_content'>
<%= question.content %>
</div>
<%= render 'upvotes/agree', :question => question %>
</div>
<div id='added_question'>
</div>
好的。因此,当我尝试加载页面时,我的 this 会抛出错误“nil:NilClass”的未定义方法“内容”。这是有道理的,因为直到出现“create-question”事件时才会定义问题。但是,我不明白为什么 Rails 试图在“create-question”事件之前渲染该部分。 (顺便说一句,我检查过,没有部分,其他一切正常,数据从 Pusher 发送)。
我发现的大多数其他推送集成示例都有相当重的客户端 JavaScript。由于我不是 JavaScript 向导,因此我希望不必为了合并推送器而重做应用程序的前端。有没有办法让我的代码保持干燥并在 Pusher 回调中使用我的部分代码?
I am working a fairly simple Rails app where people can ask questions and upvote questions. I want to build in some realtime functionality and decided to go with Pusher to do so (mainly because it seems very compatible with deploying on heroku).
Currently when you add a new question, the following bit of javascript gets executed:
if $('.question').hasClass('no_current_questions_notice')
$('.no_current_questions_notice').fadeOut 400, ->
$(this).replaceWith('<%= escape_javascript(render 'questions/added_question', :question => @added_question) %>')
$('.last_question').hide().fadeIn 400
else
$('.last_question').removeClass('last_question')
$('#added_question').replaceWith('<%= escape_javascript(render 'questions/added_question', :question => @added_question) %>')
$('.last_question').hide().fadeIn 400
I was hoping that I could simply reuse my partial in the Pusher callback so I could recreate what happens after an AJAX request for a pusher event. See below in simplest form:
<script type='text/javascript'>
var pusher = new Pusher('<%= Pusher.key %>');
var channel = pusher.subscribe('course-<%= @course.id %>');
channel.bind('create_question', function(data) {
var @added_question = data;
$('#added_question').replaceWith('<%= escape_javascript(render 'questions/added_question', :question => @added_question) %>');
});
</script>
and for reference this is my partial:
<div class='question last_question'>
<div class='question_content'>
<%= question.content %>
</div>
<%= render 'upvotes/agree', :question => question %>
</div>
<div id='added_question'>
</div>
Ok. So my this throws up the error 'undefined method `content' for nil:NilClass' right when I try to load the page. This makes sense because question isn't defined until there is a 'create-question' event. However, I don't understand why Rails is trying to render that partial before the 'create-question' event. (BTW I checked and without the partial, everything else works and the data gets sent through from Pusher).
Most other examples of pusher integration I found had fairly heavy client-side javascript. As I am no javascript wizard, I was hoping that I don't have to redo the front-end of my app just to incorporate pusher. Is there a way to keep my code DRY and use my partials in a Pusher callback?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,经过更多研究,我想我现在已经找到了解决方案。这个要点帮助我通过 Pusher 发送完全渲染的部分: https://gist.github.com/1154209 。
这种方法对我来说很有效,因为它允许我重用我的部分并合并 Pusher。我选择了要点中的选项 3,因为它允许您相对轻松地添加更多要在将来发送的数据。
Ok after some more research I think I have found a solution for now. This gist helped me send the fully rendered partial through Pusher: https://gist.github.com/1154209.
This method works for me as it allows me to reuse my partials and incorporate Pusher. I went for option 3 in the Gist because it would allow you to add more data to be sent through in the future relatively easily.