JS Function, cant figure out why
I may have one of the weirdest problems I have ever had. I have a portion of script (included below), which gets executed when the page loads. As far as I can tell, this function only gets called once, the weird part though, is the entire function executes twice.... I debugged it as much as I can. When I comment out the line where I call it (once), it doesnt get executed at all, when I uncomment it, it gets executed twice...
I dont see anywhere in the function where it executes itself, and if it did, I would think it would do it more than twice.
ANY help would be great.
Heres the small portion where it gets executed:
-
<script>
forms.sidebar_search();
</script>
And again, somehow that executes the sidebar_search() twice, but when I comment out that one single line, it wont get executed at all.
And heres the function itself.
-
sidebar_search: function() {
// Hide/Show the sidebar when the menu is collapsed
$('[data-click=sidebar-minify]').click(function(e) {
forms.sidebar_search_visibility();
});
// Make API call to get quick search setting
var quick_search_api = $.ajax({
type: "GET",
url: '/REST/app/setting/config/my_site/name/asset_quicksearch',
async: false,
dataType: 'json'
}).responseText;
console.log("RES: " + quick_search_api);
var quick_search = $.parseJSON( quick_search_api );
// Check if the quick search is enabled
if(quick_search.asset_quicksearch === true) {
// Query for all primary values of current partition
var result = $.ajax( {
type: "GET",
url: '/REST/partition/primary_attribute_values',
async: false,
dataType: 'json'
} ).responseText;
var primary_attribute = $.parseJSON( result );
// Populate the search field if values are set
if ( primary_attribute.values ) {
//$('#sidebar-search-container' ).removeClass('display-none' ).addClass('display-block');
$( '#sidebar-search-container' ).fadeIn( 'slow' );
$( '#sidebar-search' ).autocomplete( {
source: primary_attribute.values
} );
// Submit action
var do_submit = function () {
var search_val = $( '#sidebar-search' ).val();
if( $.trim(search_val) !== '') {
$( location ).attr( 'href', '/assets/search/' + primary_attribute.column + '/' + search_val );
}
};
// On enter
$( '#sidebar-search-form' ).submit( function ( e ) {
e.preventDefault();
do_submit();
} );
// On the button click
$( '#sidebar-search-form-button' ).click( function ( e ) {
e.preventDefault();
do_submit();
} );
} else {
console.log( 'No primary attribute for partition, hiding Quick Asset Search' );
//$('#sidebar-search-container' ).removeClass('display-block' ).addClass('display-none');
$( '#sidebar-search-container' ).fadeOut( 'slow' );
}
}
}
Thanks in advance for any help!