Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery Rails—在添加AJAX时减少耦合_Jquery_Ruby On Rails_Ajax_Coupling - Fatal编程技术网

Jquery Rails—在添加AJAX时减少耦合

Jquery Rails—在添加AJAX时减少耦合,jquery,ruby-on-rails,ajax,coupling,Jquery,Ruby On Rails,Ajax,Coupling,我正在创建一个小型博客应用程序来学习Rails——用户可以登录、创建帖子和评论其他用户的帖子。现在,我正在将Ajax添加到“添加您的评论”页面中,并且遇到了一些设计问题。页面流如下所示: # post section :post.title here :post.username here :post.created_at here :post.body here # comments section (sorted by date of creation): :number of c

我正在创建一个小型博客应用程序来学习Rails——用户可以登录、创建帖子和评论其他用户的帖子。现在,我正在将Ajax添加到“添加您的评论”页面中,并且遇到了一些设计问题。页面流如下所示:

# post section
 :post.title here
 :post.username here
 :post.created_at here
 :post.body here

# comments section (sorted by date of creation):
:number of comments here

:first comment
:second comment
...
:last comment

# add comment section
:text_area here
:submit button here
问题是: 1-注释按创建日期排序,因此我的ajax调用会添加新注释 到页面顶部。如果我更改排序行为,ajax代码将被破坏

2-注释div可以有3个css类:.odd、.偶数或.admin(如果注释是由管理员用户做出的,则应用循环奇数/偶数或admin)。 现在我正在硬编码这些类名,并使用一些if来获得正确的类名。这也很糟糕,如果更改css类名,ajax代码就会被破坏

我怎样才能避免这些事情,或者至少改进这些事情? 下面是我的create.js.erb:

var div = $('#comments_div');
var comment_class = div.children().first().attr('class')
var new_class;

<% if current_user.admin == 1 %>
    new_class = 'comment_body_admin'
<% else %>
if (comment_class === 'comment_body_odd')
    new_class = 'comment_body_even'
else
    new_class = 'comment_body_odd'
<% end %>

var new_div = $('#comments_div').prepend("<%= j render @comment %>");
new_div.children().first().attr('class', new_class)
var div=$('#comments_div');
var comment_class=div.children().first().attr('class'))
var新_类;
新建\u类='comment\u body\u admin'
if(comment\u class=='comment\u body\u odd')
new\u class='comment\u body\u even'
其他的
新建类='comment\u body\u odd'
var new_div=$('#comments_div')。前缀(“”);
新的子类().first().attr('class',新的类)

谢谢你的帮助

1-除了反转按顺序创建的注释外,您将如何更改注释排序?在这种情况下,需要让create.js.erb知道排序顺序,并使用append而不是prepend

2-为管理员应用“admin”类,但将奇偶条带化留给CSS。像这样的事情应该可以做到:

.comment:nth-child(2n) { background: #eeeeee; }
此外,在将节点插入DOM之前,构建节点并操纵其属性更为有效:

var comment = $("<%= j render @comment %>").addClass('comment')
comment.addClass(new_class)
$('#comments_div').prepend(comment)
var comment=$(“”)。addClass('comment'))
comment.addClass(新类)
$('#注释_div')。前置(注释)

关于第2项,我该怎么做?我有一个@comment的部分,所以偶数/奇数周期在每次ajax调用时重置。谢谢你的提示!我更新了答案。谷歌有很多关于CSS3斑马条纹的教程。