Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2012/06/18

I Like What I Did Here

I'm writing some Javascript to work this user interface I'm making. By one suggestion, you're supposed to work out the UI, set up the backend DB, and once both are as you like 'em, work on connecting them./

I am not quite there yet, but I'm close. I have n similar select boxes and n entries for each, and I would like each one to be unique, so, select box i has option i selected by default. The user can override if necessary, but in most cases, that should not be necessary.
I'm pulling each of the options from the database, so this is fairly dynamic. This is what I came up with.

// --------- --------- --------- --------- --------- --------- ---------
// fill the "i5" select boxes, and fit in order
// --------- --------- --------- --------- --------- --------- ---------
pr.fill_i5s = function ( assay ) {
    $.get( pr.url , function( data ) {
        var assay_barcodes = data.assay_barcodes ;
        var assay_data = assay_barcodes[assay].data ;
        var i5_list = assay_data.i5 ;
        $( 'select.i5' ).each( function () {
            var id = $( this ).attr( "id" ) ;
            var id_array = id.split("") ;
            var id_letter = id_array[3]  ;
            $( '#' + id ).html( '' ) ;
            for ( i in i5_list ) {
                var i5 = i5_list[i] ;
                var i5_name = i5.name ;
                var i5_bases = i5.bases ;
                var i5_array = i5_name.split("") ;
                var i5_num   = i5_array[3] ;
                if ( pr.check_i5( id_letter, i5_num ) ) {
                $( '<option/>' )
                    .text( i5_name )
                    .val( i5_name )
                    .attr( 'selected' , 'selected' )
                    .appendTo( '#' + id )
                    ;
                    }
                else {
                $( '<option/>' )
                    .text( i5_name )
                    .val( i5_name )
                    .appendTo( '#' + id )
                    ;
                    }
                }
            } ) ;
        } , 'json' ) ;
    } ;

// --------- --------- --------- --------- --------- --------- ---------
// function to move checking into a subroutine, so that i5s flow
// automatically
// --------- --------- --------- --------- --------- --------- ---------
pr.check_i5 = function ( position , barcode ) {
    if ( position == 'a' && barcode == '1' ) { return true ; }
    if ( position == 'b' && barcode == '2' ) { return true ; }
    if ( position == 'c' && barcode == '3' ) { return true ; }
    if ( position == 'd' && barcode == '4' ) { return true ; }
    if ( position == 'e' && barcode == '5' ) { return true ; }
    if ( position == 'f' && barcode == '6' ) { return true ; }
    if ( position == 'g' && barcode == '7' ) { return true ; }
    if ( position == 'h' && barcode == '8' ) { return true ; }
    return( false ) ;
    } ;

I perhaps should use a case statement, but honestly, I haven't used one of those in production code in years, and I'm not sure that you can case off two variables. I suspect I should use === for strong typing, but I can always change that. I know that, if I had ands and ors like this in the center of the first subroutine, it would be ugly. As is, I like this.