gpt4 book ai didi

ruby-on-rails - 从javascript触发茧add_association

转载 作者:行者123 更新时间:2023-12-04 21:42:26 25 4
gpt4 key购买 nike

我正在使用 cocoon,我想在选择日期选择器中的日期(可以是多个日期)时自动添加子记录。

我可以像这样在coffeescript中捕获日期选择,但我不知道如何通过cocoon添加子记录,即通过模拟触发link_to_add_association时发生的情况。

$(".form_multidate").datepicker().on 'changeDate', (e) -> alert(e.dates)

茧设置是一个标准的嵌套形式,没有技巧,在页面上工作正常。

编辑:评论重新绑定(bind)日历中提到的代码:
$(document).ready(function() {
$('#other_request_details')
.bind('cocoon:after-insert', function() {
return $('.datepicker-single').datepicker({
dateFormat: "DD, dd M yy"
});
});
});

最佳答案

调用 JS 函数以使用 Cocoon 添加新记录是不可能的。你唯一能做的就是触发添加关联按钮的点击事件。

If you see Cocoon's library code you will see that all new record functionality is bound to the click button

 $(document).on('click', '.add_fields', function(e) {
e.preventDefault();
var $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') || 'before',
insertionNode = $this.data('association-insertion-node'),
insertionTraversal = $this.data('association-insertion-traversal'),
count = parseInt($this.data('count'), 10),
regexp_braced = new RegExp('\\[new_' + assoc + '\\](.*?\\s)', 'g'),
regexp_underscord = new RegExp('_new_' + assoc + '_(\\w*)', 'g'),
new_id = create_new_id(),
new_content = content.replace(regexp_braced, newcontent_braced(new_id)),
new_contents = [];


if (new_content == content) {
regexp_braced = new RegExp('\\[new_' + assocs + '\\](.*?\\s)', 'g');
regexp_underscord = new RegExp('_new_' + assocs + '_(\\w*)', 'g');
new_content = content.replace(regexp_braced, newcontent_braced(new_id));
}

new_content = new_content.replace(regexp_underscord, newcontent_underscord(new_id));
new_contents = [new_content];

count = (isNaN(count) ? 1 : Math.max(count, 1));
count -= 1;

while (count) {
new_id = create_new_id();
new_content = content.replace(regexp_braced, newcontent_braced(new_id));
new_content = new_content.replace(regexp_underscord, newcontent_underscord(new_id));
new_contents.push(new_content);

count -= 1;
}

var insertionNodeElem = getInsertionNodeElem(insertionNode, insertionTraversal, $this)

if( !insertionNodeElem || (insertionNodeElem.length == 0) ){
console.warn("Couldn't find the element to insert the template. Make sure your `data-association-insertion-*` on `link_to_add_association` is correct.")
}

$.each(new_contents, function(i, node) {
var contentNode = $(node);

insertionNodeElem.trigger('cocoon:before-insert', [contentNode]);

// allow any of the jquery dom manipulation methods (after, before, append, prepend, etc)
// to be called on the node. allows the insertion node to be the parent of the inserted
// code and doesn't force it to be a sibling like after/before does. default: 'before'
var addedContent = insertionNodeElem[insertionMethod](contentNode);

insertionNodeElem.trigger('cocoon:after-insert', [contentNode]);
});
});

关于ruby-on-rails - 从javascript触发茧add_association,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22998259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com