Skip to content

Instantly share code, notes, and snippets.

@guipdutra
Last active December 17, 2015 07:49
Show Gist options
  • Save guipdutra/5575488 to your computer and use it in GitHub Desktop.
Save guipdutra/5575488 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
function loadCapabilityFromBudgetAllocation(event, budget_allocation) {
if (budget_allocation) {
var capabilities = budget_allocation.capabilities;
if (capabilities.length == 1) {
$(this).closest('.nested-extra_credit_moviment_types').find('.capability').val(capabilities[0].description);
$(this).closest('.nested-extra_credit_moviment_types').find('.capability').next('input').val(capabilities[0].id);
$(this).closest('.nested-extra_credit_moviment_types').find('.capability').attr('disabled','disabled');
} else {
disableOrEnableCapability($(this), true);
$(this).closest('.nested-extra_credit_moviment_types').find('.capability').data('modal-url', '<%= modal_capabilities_path %>?by_budget_allocation_id='+ budget_allocation.id);
}
} else {
disableOrEnableCapability($(this), true);
}
}
function cleanCapability(budget) {
budget.closest('.nested-extra_credit_moviment_types').find('.capability').val('');
budget.closest('.nested-extra_credit_moviment_types').find('.capability').next('input').val('');
}
function disableOrEnableCapability(budget, clean) {
if (clean) {
cleanCapability(budget);
}
if (budget.val() == '') {
budget.closest('.nested-extra_credit_moviment_types').find('.capability').attr('disabled','disabled');
} else {
budget.closest('.nested-extra_credit_moviment_types').find('.capability').removeAttr('disabled');
}
}
$('.nested-extra_credit_moviment_types').on('change', '.budget-allocation', loadCapabilityFromBudgetAllocation);
$.each($('.budget-allocation'), function(i, v) {
disableOrEnableCapability($(v));
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment