Name

Description

SK.DB.Format( )

Constructor

This library is used for formatting of data received from the server and sent to the server. Usually the data from the server is good enough for user presentation, but some types of data are looking better when formatted into a human readable form.

This library provides means for formatting all types of columns specified in SK.DB.Column. The formatting of each of the column types can be redefined if needed. 

If there is a need to redefine a format the following class interface should be implemented:
Extends: SK.DB.Format.Default (should inherit that class)
Constructor: function( column_meta ) — the meta information of a single column (see SK.DB.getColumns for example)
Methods: 

- generateColumnCSSClass( ) — (optional) should return a valid CSS "class" attribute value which is used when displaying the value of the current column type

- toClient( value ) — (optional) formats the data from the server and returns the formatted result

- toServer( value ) — (optional) formats the value that comes from the client and will be passed back to the server


Example of such a class is:

SK.DB.Format.Password = new Class({
   Extends: SK.DB.Format.Default,
   "type": "password",
   
   toClient: function( value ) {
      return '<span class="' +
                this.generateColumnCSSClass( ) +
             '">**********</span>';
   }
});

setColumnHandler(
   column_type,
   handler
)

The column type is one of the constants in SK.DB.Column. The "handler" must be the name of the class that inherits SK.DB.Format.Default

The following predefined handlers are available for the appropriate column types:
SK.DB.Format.Text
SK.DB.Format.Number
SK.DB.Format.Date
SK.DB.Format.RichText
SK.DB.Format.Password
SK.DB.Format.File
SK.DB.Format.ID
SK.DB.Format.Checkbox
SK.DB.Format.Dropdown
SK.DB.Format.Multichoice
SK.DB.Format.Combo


All of them extend the SK.DB.Format.Default class.

getColumnHandler(
   column_meta
)

Returns the JS class that takes care of the columns of type stored in the "column_meta". The column_meta is the same meta information given by the db.column.get API call or by the SK.DB.getColumn

toClient(
   column_meta,
   value
)

Uses the handler for the type of column stored in the "column_meta" and invokes its "toClient" method. If no custom handler specified, a predefined handler is used. Returns the value of handler's "toClient"

toServer(
   column_meta,
   value
)

Uses the handler for the column type stored in "column_meta" and invokes the "toServer" method. Returns the value of handler's "toServer".

 

Examples
Formatting the records from a random database in an account

<script type="text/javascript">
   var MyFormatRecords = new Class({
      print_callback: function () {}

      session_id: '',
      
      initialize: function( print_callback ) {
         this.session_id     = SK.Singletons.env.get( 'session_id' );
         this.print_callback = print_callback;
         
         new SK.DB( this.session_id ).getDatabases(
            this.getRandomDatabase.bind( this ),
            this.failure.bind( this )
         );
      },
      
      getRandomDatabase: function( databases ) {
         var db_id = databases.getRandom( ).id;
         
         new SK.DB( this.session_id ).getRecords(
            db_id,
            null, // All columns
            null, // No filter, i.e. get all records
            this.displayRecords.bind( this ),
            this.failure
         );
      },
      
      displayRecords: function( columns, records, num_records ) {
         this.print( "Columns: " + columns.map( function( c ) { return c.name; } ).join( ", " ) );
         
         var format_obj = new SK.DB.Format( );
         
         // Generate a hash by column name. The value will be the column type.
         // This way the formatting method can have an idea how to format the
         // column and this will work well.
         var columns_map = {};
         for ( var i = 0; i < columns.length; i++ ) {
            columns_map[ columns[ i ].name ] = columns[ i ];
         }
         
         for ( var i = 0; i < records.length; i++ ) {
            var record = records[ i ];
            for ( var col_name in record.cells ) {
               var value = record.cells[ col_name ];
               this.print( "<table><tr><td>" + col_name + "</td><td>" + format_obj.toClient( columns_map[ col_name ], value ) + "</td></tr></table>" );
            }
            this.print( "--------------------" );
         }
      },
      
      failure: function( message ) {
         alert( message );
      },
      
      print: function( message ) {
         this.print_callback( message );
      }
   });
   
   new MyFormatRecords( function( message ) {
      _$( 'mylog' ).grab( new Element( 'div', { html: message } ) );
   });
</script>
<style>
   /* The formatting of a few different types of columns is here */
   #mylog .db-column-type {
      font-weight: bold;
      color: #000000;
   }
   
   #mylog .db-column-type-number {
      color: #0000FF;
   }
   
   #mylog .db-column-type-date {
      color: #00FF00;
   }
   
   #mylog .db-column-type-id {
      color: #FF0000;
   }
</style>
<div id="mylog"></div>