fwCursor Object

Description

The fwCursor class can be used as a base class for objects that are to be serialized to JSON format with the fwJSON.Write method for downloading to an Ajax client-side script. The output produced, when an object based on this class is serialized, is identical to the output that the fwJSON.WriteCursor method would produce for the same cursor, or table. Scripts would utilize this class in instances, where the JSON response needs to include data from multiple cursors, or additional data values. In such cases, the value passed to the fwJSON.Write method would be an object, or array, containing one, or more objects based on the fwCursor class.


Constructor

Syntax

MyObject = CREATEOBJECT("fwCursor", [TableAlias], [NumberOfRecords])

Parameters

WorkArea | TableAlias

Optional string value, used to populate the TableAlias property.

NumRecords

Optional numeric value, used to populate the NumberOfRecords property.

Example

The following script reads a search term from the query string, searches two tables and then sends the results in JSON format to the browser.

<%
LOCAL SalesPersonName, JSONResponse
* Get the sales person name from the query string
SalesPersonName = Request.QueryString("SalesPersonName")
JSONResponse = CREATEOBJECT("CUSTOM")
JSONResponse.AddProperty("FoundSalesPerson", .F.)
JSONResponse.AddProperty("FoundSales", .F.)
* Search for sales person
SELECT * ;
        FROM SalesPersons;
        WHERE name LIKE "SMITH" ;
        INTO CURSOR CursorSalesPerson
IF RECCOUNT() > 0
    * Found the requested sales person
    JSONResponse.FoundSalesPerson = .T.
    JSONResponse.AddProperty("SalesPersonData")
    JSONResponse.SalesPersonData = CREATEOBJECT("fwCursor", "CursorSalesPerson"))
    * Search for sales records, related to the requested sales person
    SELECT Sales.*;
            FROM Sales ;
            JOIN SalesPerson ON Sales.SalesPersonId = SalesPerson.SalesPersonId ;
            WHERE SalesPerson.name LIKE "SMITH" ;
            INTO CURSOR CursorSales
    IF RECCOUNT() > 0
        JSONResponse.FoundSales = .T.
        JSONResponse.AddProperty("SalesData")
        JSONResponse.SalesData = CREATEOBJECT("fwCursor", "CursorSales"))
    ELSE
ENDIF
Response.ContentType = "text/plain"
Response.Write(fwJSON.Write(JSONResponse))
%>

Properties

TableAlias The work area, or alias of the table/cursor to be serialized.
NumberOfRecords The number of records to output.

Methods

ToJSONString Serializes the contents of cursor/table into a JSON string.

TableAlias Property

Description

The TableAlias property sets/gets the work area, or alias of the table/cursor to be serialized. The table/cursor must be open at the time when objects, based on the fwCursor class are to be serialized. If this property is not set, then the serialization code uses the table/cursor in the currently selected work area. Typically this property gets populated via a parameter passed to the constructor, when the object is created with the CREATEOBJECT command. This property can be set to either a string value (alias), or a numeric value (work area).


NumberOfRecords Property

Description

The NumberOfRecords property sets/gets the number of records to output and has a default value is 0. If NumberOfRecords is 0, all records are output. If NumberOfRecords is greater than the number of records remaining in the table, all remaining records are output. Typically this property gets populated via a parameter passed to the constructor, when the object is created with the CREATEOBJECT command.


ToJSONString Method

Description

The ToJSONString method serializes a VFP data value into a JSON string. It is typically used in Ajax applications to return data to browser-side JavaScript programs. Typically scripts don't call this method, but rather use the fwJSON.Write method to serialize a container array, or object.

Syntax

MyObject.ToJSONString()


© Aegis Group