想要在每次触发创建注释标题的AJAX调用时评估if语句 [英] Want to evaluate an if statement every time an AJAX call for creating comment_titles is triggered

查看:72
本文介绍了想要在每次触发创建注释标题的AJAX调用时评估if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视频节目视图中有以下if语句:

<% if @video.user == current_user && current_user != nil && @video.comment_titles.count < 3 %>
    <%= link_to "Add Comment Title", "#", :id => "comment_title_link", :class => "edit" %>
    <%= simple_form_for @video, :remote => true do |f| %>
     <%= f.input :comment_title_names, :label => false, :placeholder => "Add a Comments Title" %>
      <%= f.button :submit, :value => 'Add', :id => 'add_comment_title' %>
      <div class='hint'>Let your listeners know what comments you want by adding a guiding title for them. Pose a question, ask for feedback, or anything else!</div>
    <% end %>
<% elsif @video.comment_titles.count == 3 && @video.user == current_user && current_user != nil  %>
    <p> You have reached your limit of comment titles. You can always add a new one by deleting one of your old ones. </p>
<% end %>

此if语句实质上是对@video.comment_titles.count求值,以确定if语句或elsif语句是否为true.我让用户使用ajax添加comment_titles,因此当@video.comment_titles.count == 3为true时,由于仅在重新加载页面后调用if语句,所以它不能正确地评估if语句. >

我希望每次comment_titles的数量更改时都动态调用if语句,这等效于每当触发更新注释标题的AJAX调用时说一次.但是,我宁愿在客户端执行此操作,也不必在.js.erb文件中执行此操作.我将如何在客户端触发此操作?

好的,所以没有人回答,所以我假设我没有提供足够的代码,我不清楚我要做什么,或者这是不可能的.是哪一个?

解决方案

在牢记您的评论再次阅读了您的问题之后,我认为

  • 对于事件已添加新评论标题",有一个javascript事件处理程序.
  • 提供的片段与例如<div id="add_comment_title_action">fragment</div>
  • 之类的容器一起驻留
  • 每次执行事件处理程序时都应刷新片段
  • 您已经看到可以通过$('#add_comment_title_action').load('fragment_url')加载片段,但是您只需要在客户端内完成

让我们假设事件处理程序看起来像

$('#add_comment_title_form').submit(function(e) {
   e.preventDefault();
   $.post( 'url',
          { 'title': 'Lorem ispum dolor sit amet'}
   );
   updateFragment();       
});

然后您可以使用一个功能来更新片段

updateFragment() {
  // Let's assume that @video.user == current_user && !current_user.nil?
  if($('.comment_title').size() < 3) {
     // NOP add something here to handle case when a title is deleted
  } else {
     $('#add_comment_title_action').html('<p> You have reached your limit of comment titles. You can always add a new one by deleting one of your old ones. </p>'); 
  }
}

如何使代码更好:

  • 应该有一些回调,以检查发布请求是否成功,并相应地进行
  • 您可以通过简单地隐藏和显示取决于状态的选项来处理片段的两个不同状态(如果可以假设所有用户都启用了javascript和css)
  • 如果您决定用.html(...)替换片段,则片段中元素的事件处理程序应与.live(event, handler)绑定,以便您可以在执行删除操作后添加新的注释标题

请注意,在客户端进行的每项检查也必须在服务器端进行.

查看历史记录以获得完全不同的答案:)

I have this if statement in my video show view:

<% if @video.user == current_user && current_user != nil && @video.comment_titles.count < 3 %>
    <%= link_to "Add Comment Title", "#", :id => "comment_title_link", :class => "edit" %>
    <%= simple_form_for @video, :remote => true do |f| %>
     <%= f.input :comment_title_names, :label => false, :placeholder => "Add a Comments Title" %>
      <%= f.button :submit, :value => 'Add', :id => 'add_comment_title' %>
      <div class='hint'>Let your listeners know what comments you want by adding a guiding title for them. Pose a question, ask for feedback, or anything else!</div>
    <% end %>
<% elsif @video.comment_titles.count == 3 && @video.user == current_user && current_user != nil  %>
    <p> You have reached your limit of comment titles. You can always add a new one by deleting one of your old ones. </p>
<% end %>

This if statement essentially evaluates @video.comment_titles.count to determine if the if statement or the elsif statement is true. I let users add comment_titles with ajax and so by the time @video.comment_titles.count == 3 is true, it won't correctly evaluate the if statement since the if statement, which is in my video show view, is only called after a page reload.

I want the if statement to be called dynamically every time the number of comment_titles changes, which is equivalent to saying whenever the AJAX call for updating comment_titles is triggered. However, I'd rather do this on the client side than have to do it in a .js.erb file. How would I trigger this on the client side?

OK so no one has answered, so I'm assuming either I have not provided enough code, I have not been clear in what I am trying to do, or it is impossible. Which is it?

解决方案

After reading your question again with your comment in mind, I assume that

  • There is a javascript event handler for the event 'new comment title has been added'.
  • The provided fragment resides withing a container of sort e.g.<div id="add_comment_title_action">fragment</div>
  • The fragment should be refreshed every time the event handler is executed
  • You've seen that is possible to load the fragment by $('#add_comment_title_action').load('fragment_url') but you rather just do it within the client

Let's assume that the event handler looks something like

$('#add_comment_title_form').submit(function(e) {
   e.preventDefault();
   $.post( 'url',
          { 'title': 'Lorem ispum dolor sit amet'}
   );
   updateFragment();       
});

Then you could have a function for updating the fragment

updateFragment() {
  // Let's assume that @video.user == current_user && !current_user.nil?
  if($('.comment_title').size() < 3) {
     // NOP add something here to handle case when a title is deleted
  } else {
     $('#add_comment_title_action').html('<p> You have reached your limit of comment titles. You can always add a new one by deleting one of your old ones. </p>'); 
  }
}

How the code can be made better:

  • There should be some callback to check if the post request has succeeded and proceed accordingly
  • You could handle the two different states of the fragment by simply hiding and showing the options depending on the state (if you can assume all the users have enabled javascript and css)
  • If you decide to replace the fragment with .html(...) then the event handlers for the elements within the fragment should be bound with .live(event, handler) so that you can enable adding new comment titles after a delete has been performed

Note that every check done in the client side must be done in the server side also.

See history for completely different answer :)

这篇关于想要在每次触发创建注释标题的AJAX调用时评估if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆