如何正确地将属性“association-insertion-node”作为函数传递?

时间:2017-03-29 12:28:11

标签: coffeescript cocoon-gem

cocoon文档Control the Insertion Behaviour中示例中的链接是什么 我收到一个错误“...未定义”,如果我传递一个DOM对象,我得到'data-association-insertion-node =“[object,Object]”'     代码:

$(".add-comment").on 'cocoon:before-insert', (e, object) ->
  wlink = $(this).children('a')
  wlink
    .attr(
      "data-association-insertion-node",
      (link) ->
        link.parents('.object-name').children('.comments')
    )

更新

实际上,它没有,它只有一个嵌套,并谈到多个嵌套我有一个在另一个部分,它工作正常,但在那里我使用在插入前在js中生成的ID,所以我有能力设置association-insertion-node as a string。 但在这种情况下,我没有这样的机会,所以这就是我选择遍历方法的原因。

所以我说的是我没听清楚它是如何运作的

function(link){
    return link.closest('.row').next('.row').find('.sub_tasks_form')
}

因为我得到了

TypeError: link.parents is not a function.
 (In 'link.parents('.object-name')', 'link.parents' is undefined)`

似乎必须声明它并不是,也许还有另一个链接入口点,也许它因咖啡而无效。

如果我通过$(this),我会

<a data-association-insertion-method="append" 
class="add_fields" data-association="comment" 
data-associations="comments" 
data-association-insertion-template="Generated HTML" 
href="#" data-association-insertion-node="[object Object]"> add comment</a>

我想要的是通过DOM遍历将评论发布到相关对象。

P.S。

非常感谢您的关注。

1 个答案:

答案 0 :(得分:0)

  

我使用的是数据而不是attr但是imho是完全相同的。 - nathanvda 3月30日8:38

很重要。从attr变为数据后开始工作。