Chapter 2. Executing JavaScript in Open Data Tables

In this Chapter:

Introduction

Features and Benefits

The ability to execute JavaScript extends the functionality of Open Data Tables in many ways, including the following:

  • Flexibility beyond the normal templating within Open Data Tables: Executing JavaScript allows you to use conditional logic and to format data in a granular manner.
  • Better data shaping and parsing: Using JavaScript, you can take requests and responses and format or shape them in way that is suitable to be returned.
  • Better support for calling external Web services: Some Web services use their own security and authentication mechanisms. Some also require authentication headers to be set in the Web service request. The execute element allows you to do both.
  • Better support for adding, modifying, and deleting data using external Web services: For Web services that support write access, YQL allows you to insert, update, and delete using server-side JavaScript within the insert, update, and delete elements, which are nested within the binding element.
  • Ability to make asynchronous calls: YQL provides JavaScript methods for making REST calls that take a callback function parameter to handle the returned response. Using these methods with callback functions, you can initiate multiple calls that won't be blocked by a slow response or a timed-out request.

The ability to execute JavaScript is implemented through the execute sub-element within an Open Data Table definition.

Within the execute sub-element, you can embed JavaScript and E4X (the shortened term for ECMAScript for XML), which adds native XML support to JavaScript. Support for E4X was first introduced in JavaScript 1.6.

When a YQL statement calls an Open Table Definition that contains the execute sub-element, YQL no longer performs the request to the templated URI in the endpoint. Instead YQL provides a runtime environment in which the JavaScript is executed server-side. Your JavaScript in turn must then return data as the output to the original YQL statement.

Table of Contents