Skip to content

Instantly share code, notes, and snippets.

@gwincr11
Created December 3, 2013 23:41
Show Gist options
  • Save gwincr11/7779775 to your computer and use it in GitHub Desktop.
Save gwincr11/7779775 to your computer and use it in GitHub Desktop.
Current vs. Ideal has many workflow for Active Admin
//current workflow
//Inside of callback function on file upload completion
var $container = $(".advertising_entities");
$container.find(".has_many_add").trigger("click");
//better workflow
var $container = $(".advertising_entities");
var $has_many_element = $container.find(".has_many_add");
activeAdmin.api.form.add_has_many($has_many_element);
//needed changes
//API structure debatable just for example:
function activeAdmin() {
return {
api: {
form: {
add_has_many: function($elem){
parent = elem.closest('.has_many_container');
parent.trigger(before_add = $.Event('has_many_add:before'));
if (!before_add.isDefaultPrevented()) {
index = parent.data('has_many_index') || parent.children('fieldset').length - 1;
parent.data({
has_many_index: ++index
});
regex = new RegExp(elem.data('placeholder'), 'g');
html = elem.data('html').replace(regex, index);
fieldset = $(html).insertBefore(this);
recompute_positions(parent);
return parent.trigger('has_many_add:after', [fieldset]);
}
}
}
}
}
}
//Decoupled code calls api
$(document).on('click', 'a.button.has_many_add', function(e) {
var before_add, elem, fieldset, html, index, parent, regex;
e.preventDefault();
activeAdmin.api.form.add_has_many($(this));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment