Klipfolio Dashboard 5 API

Object Engines.Data

Object
   |
   +--Klip
         |
         +--Engines
               |
               +--Engines.Data

The Engines.Data is the main interface to Klipfolio Dashboard's built-in XML parsing engine. The most common use is to call process() with a URL to data.

Example Klip: Parsing Two Items

To show how process() updates the Klip, the following sample Klip uses Klip.trace() to output incoming items in the Debug Window as they are processed by the XML parser.
<klip>
   <identity>
      <title>
         API: Engines.Data
      </title>
   </identity>
   <locations>
      <contentsource>
        http://support.klipfolio.com/files/demo/demo.xml
      </contentsource>
      <icon>
         http://www.klipfolio.com/static/klips/klipfolio/sample_icon.png
      </icon>
      <banner>
         http://www.klipfolio.com/static/klips/klipfolio/sample_banner.png   
      </banner>
   </locations>
   <style>
      alert {
         type: item;
      }

      sender {
         itemcol: 2;
         noterow: 1;

         label: 'Sender';

         emphasis: strong;
      }


      summary {
         itemcol: 3;
         noterow: 4;

         wrap: false;

         notelabel: false;
      }

      date {
         itemcol: 4;
         noterow: 2;

         label: 'Date';
      }

      category {
         noterow: 3;

         label: 'Category';
      }


      level {
         itemcol: 1;
         noterow: 5;
         notelabel: false;
         
         type: image;
      }

      url {
         type: link;
      }
            
      id {
         key: override;
      }

   </style>
   <klipscript>
   <![CDATA[


function onRefresh()
{

   // Define XML Parser callbacks
   Engines.Data.onCreate = myCreate;
   Engines.Data.onUpdate = myUpdate;

   _t( "onRefresh () -- " + Prefs.contentsource );
   var result = Engines.Data.process ( Prefs.contentsource );

   show_state();
   
   return result;
}


//
// Support functions -----------------------------------------------
//

function myCreate( new_item )
{
   _t( "myCreate() --- Got an XML Callback for new item" );
   return modify (new_item);
}

function myUpdate( current_item, properties )
{
   _t( "myUpdate() --- Got an XML Callback for updated item" );
   return modify (properties);
}

function modify( pending )
{
   // Place your code here to modify incoming/updating items

   _t( "    sender: " + pending.getData( "sender" ));
   _t( "    summary: " + pending.getData( "summary" ));
   _t( "    date: " + pending.getData( "date" ));
   _t( "    level: " + pending.getData( "level" ));
   _t( "    category: " + pending.getData( "category" ));
   _t( "    id: " + pending.getData( "id" ));

   _t( " " );
   return true;
}

//
// Useful Debug Code -----------------------------------------------
//

function show_state() 
{
   show_items ();
}

function show_items() 
{
   if (Items.length == 0) 
   {
      trace ("There are no items in the Items[] array\r\n");
      return;
   }

   for( i = 0; i < Items.length; i++ ) 
   {
      trace ("Items[" + i + "]: \r\n");       
      queryItem (Items[i]);
   }
}

function queryItem( item ) 
{
    trace( "-----------------\r\n" );
    trace( "    .text         = \"" + item.text + "\"  (string)\r\n" );
    trace( "    .url          = \"" + item.url + "\"  (string)\r\n" );
    trace( "    .note         = \"" + item.note + "\"  (string)\r\n" );
    trace( "    .iid          = \"" + item.iid + "\"  (string)\r\n" );
    trace( "    .extra        = \"" + item.extra + "\"  (string)\r\n" );
    trace( "    .hidden       = " + item.hidden + " (boolean)\r\n" );
    trace( "    .visited      = " + item.visited + " (boolean)\r\n" );
    trace( "    .canpurge     = " + item.canpurge + "  (boolean)\r\n" );
    trace( "    .alerting     = " + item.alerting + " (boolean)\r\n" );
    trace( "    .creation     = " + item.creation + " (double)\r\n" );
    trace( "    .pubdate      = " + item.pubdate + "  (double)\r\n" );
    trace( "    .lastmodified = " + item.lastmodified + "  (double)\r\n" );

    // Output the current value of each defined element in an item

       trace( "      .getData( \"sender\" ) = \"" + item.getData( "sender" ) + "\" (string)\r\n" );
       trace( "      .getData( \"summary\" ) = \"" + item.getData( "summary" ) + "\" (string)\r\n" );
       trace( "      .getData( \"date\" ) = \"" + item.getData( "date" ) + "\" (string)\r\n" );
       trace( "      .getData( \"url\" ) = \"" + item.getData( "url" ) + "\" (string)\r\n" );
       trace( "      .getData( \"level\" ) = \"" + item.getData( "level" ) + "\" (string)\r\n" );
       trace( "      .getData( \"category\" ) = \"" + item.getData( "category" ) + "\" (string)\r\n" );
       trace( "      .getData( \"id\" ) = \"" + item.getData( "id" ) + "\" (string)\r\n" );
    
    trace( "\r\n" );
}


var gTrace = true;
function _t( s ) 
{
   if ( gTrace ) {
      trace( s + "\r\n" );
   }
}
   ]]>
   </klipscript>
</klip>


     	

   

XML Callbacks

The process() function does all work of extracting items and creating an Item object from the data. Sometimes you need to modify the properties of the Item before they are added to the Items object.

Klipfolio Dashboard provides hooks that you can define handler functions that calls your JavaScript function with the Item before it's added to the Klip. In addition to modifying any of the Item's properties, you can tell process() to discard the item by returning false from the callback.

The following figure shows the callbacks available.

Figure: XML callbacks from Engines.Data.process()

Use onBeginProcess() to do any global changes to the data before the processing. Use onUpdate() and onCreate() to modify the extracted data before it's applied to the Items[] array. Use onEndProcess() to do anything else before process() exits.




Properties Summary
 integer autosort
          Specifies whether process() should automatically sort processed items according to pubdate value (if available) in source (default true).
 String format
          Specifies the format of the input source as CSV or TSV.
 boolean ignoreencoding
          Specifies that Klipfolio Dashboard's XML processor should ignore the encoding when processing data.
 integer itemdepth
          Used within onCreate() and onUpdate() to determine the depth of the currently matching item as defined in the Klip's <style>.
 String stylesheet
          Specifies the stylesheet for this Klip.
   
Function Summary
 function applyStyle( <String> URL_or_data, <String> stylesheet )
           Parses the specified data according to a stylesheet without creating items and returns an array of items.
 String getScratch( <String> xml_element )
           Returns the latest data for xml_element, an XML element defined as type: scratch in the Klip's <style> block.
 String onBeginProcess( <String> text )
           Defines a JavaScript callback that process() will be called before the XML parser begins to process data using the specifications in <style>.
 boolean onCreate( <Item> new_item )
           Assigns a JavaScript function to call just before adding a new item to the Items array.
 boolean onEndProcess( boolean | string )
           Defines a JavaScript callback that process() will call just before it exits.
 boolean onUpdate( <Item> existing_item, <String> properties )
           Defines a JavaScript callback just before process() updates an existing Item object with incoming data.
 boolean process( <String> URL_or_data | <Engines.HTTP.HTTPRequest> httpRequestObject | <Engines.HTTP.HTTPResponse> httpResponseObject [, <integer> index] )
           Parses XML and adds the results to the Klip.

Properties Detail

autosort

integer autosort

format

String format

ignoreencoding

boolean ignoreencoding

itemdepth

integer itemdepth

stylesheet

String stylesheet

Function Detail

applyStyle

function applyStyle( <String> URL_or_data, <String> stylesheet )

getScratch

String getScratch( <String> xml_element )

onBeginProcess

String onBeginProcess( <String> text )

onCreate

boolean onCreate( <Item> new_item )

onEndProcess

boolean onEndProcess( boolean | string )

onUpdate

boolean onUpdate( <Item> existing_item, <String> properties )

process

boolean process( <String> URL_or_data | <Engines.HTTP.HTTPRequest> httpRequestObject | <Engines.HTTP.HTTPResponse> httpResponseObject [, <integer> index] )

Klipfolio Dashboard 5 API

© 2009 Klipfolio Inc. All Rights Reserved.