Ruby on rails 从javascript触发cocoon添加_关联
我正在使用cocoon,我想在选择日期选择器中的日期(可以是多个日期)时自动添加子记录 我可以像这样在coffeescript中捕获日期选择,但我不知道如何通过cocoon添加子记录,即通过模拟触发链接到添加关联时发生的情况Ruby on rails 从javascript触发cocoon添加_关联,ruby-on-rails,ruby-on-rails-4,coffeescript,cocoon-gem,Ruby On Rails,Ruby On Rails 4,Coffeescript,Cocoon Gem,我正在使用cocoon,我想在选择日期选择器中的日期(可以是多个日期)时自动添加子记录 我可以像这样在coffeescript中捕获日期选择,但我不知道如何通过cocoon添加子记录,即通过模拟触发链接到添加关联时发生的情况 $(".form_multidate").datepicker().on 'changeDate', (e) -> alert(e.dates) cocoon设置是标准的嵌套形式,没有技巧,在页面上工作良好 编辑:注释重新绑定日历中提到的代码: $(document
$(".form_multidate").datepicker().on 'changeDate', (e) -> alert(e.dates)
cocoon设置是标准的嵌套形式,没有技巧,在页面上工作良好
编辑:注释重新绑定日历中提到的代码:
$(document).ready(function() {
$('#other_request_details')
.bind('cocoon:after-insert', function() {
return $('.datepicker-single').datepicker({
dateFormat: "DD, dd M yy"
});
});
});
调用JS函数以添加Cocoon的新记录是不可能的。您唯一能做的就是触发addassociation按钮的click事件
$(文档)。在('单击','上。添加字段',函数(e){
e、 预防默认值();
变量$this=$(this),
assoc=$this.data('association'),
assocs=$this.data('associations'),
content=$this.data('association-insertion-template'),
insertionMethod=$this.data('association-insertion-method')| |$this.data('association-insertion-position')|在“,
insertionNode=$this.data('association-insertion-node'),
insertionTraversal=$this.data('association-insertion-traversal'),
count=parseInt($this.data('count'),10),
regexp\u括号=new regexp('\\[new\'+assoc+'\\](.*?\\s)'g'),
regexp\u下划线=new regexp(“\u new”+assoc+”\uw*,“g”),
新建\u id=创建\u新建\u id(),
new_content=content.replace(regexp_大括号,newcontent_大括号(new_id)),
新内容=[];
如果(新内容==内容){
用括号括起来的regexp=newregexp('\\[new\'+assocs+'\\](.*?\\s)'g');
regexp_下划线=new regexp(“_new_'+assocs+'”)(\\w*),“g”);
new_content=content.replace(regexp_括号,newcontent_括号(new_id));
}
new_content=new_content.replace(regexp_下划线,newcontent_下划线(new_id));
新内容=[新内容];
count=(isNaN(count)?1:Math.max(count,1));
计数-=1;
同时(计数){
new_id=创建_new_id();
new_content=content.replace(regexp_括号,newcontent_括号(new_id));
new_content=new_content.replace(regexp_下划线,newcontent_下划线(new_id));
新内容。推送(新内容);
计数-=1;
}
var insertionNodeElem=getInsertionNodeElem(insertionNode,insertionTraversal,$this)
如果(!InsertionNodeLem | |(InsertionNodeLem.length==0)){
console.warn(“找不到要插入模板的元素。请确保'link\u to\u add\u association'上的'data association insertion-*`正确。”)
}
$.each(新内容、函数(i、节点){
var contentNode=$(节点);
insertionNodeElem.trigger('cococoon:before insert',[contentNode]);
//允许任何jQueryDOM操作方法(之后、之前、追加、前置等)
//在节点上调用。允许插入节点作为插入节点的父节点
//代码,而不是像after/before那样强制它成为同级。默认值:“before”
var addedContent=insertionNodeElem[insertionMethod](contentNode);
insertionNodeElem.trigger('cococoon:after insert',[contentNode]);
});
});你有没有想过?没有,我最后做了一些不同的事情。我怀疑你需要绑定到一个cocoon方法。我使用了类似于重新加载jquery日历的方法:只需在链接上触发click事件,即可添加关联元素,应该这样做吗?