MillionMunkeys.net

 

PiMunkey Documentation -> Pi Object Reference ->

Getter Functions

 

  • exists: Returns a boolean value indicating whether or not the specified property exists within the object.

    Usage:

    1. exists (integer index) : Indicates whether or not an item exists at the specified position in the object.
    2. exists (string propertyName) : Indicates whether or not there is an item associated with the given name.

    Examples:

    ColdFusion:
    <cfif object.exists(1)>
         <cfset object.insertAt(1, newItem) />
    <cfelse>
         <cfset object.add(newItem) />
    </cfif>

    <cfif not object.exists("url") and object.exists("id")>
         <cfset object.set("url", "#this.get('baseURL')#?#this.get('urlVariable')#=#object.get('id')#") />
    </cfif>

    Javascript:
    if ( object.exists(1) )
         object.insertAt(1, newItem);
    else
         object.add(newItem);

    if ( !object.exists("url") && object.exists("id") )
         object.set( "url", this.get('baseURL') + "?" + this.get('urlVariable') + "=" + object.get('id') );

  • get: Returns either the value of a property, the item at the specified index, or the empty string if nothing found.

    Usage:

    1. get (string propertyName) : Pass a string to the "get" function and it will return the item associated with that property name. If no item exists, it will return the empty string.
    2. get (integer index) : Pass an integer to the "get" function and it will return the item at that position in the object. If no item exists, it will return the empty string.

    Examples:

    ColdFusion:
    <cfloop from="1" to="#object.getLength()#" index="i">
         <cfset nextItem = object.get(i) />
         <cfset titleList = ListAppend(titleList, nextItem.get("title")) />
    </cfloop>
    <cfreturn titleList />

    Javascript:
    var nextItem, titles=[];
    for (var i=0; i < object.getLength(); i++) {
         nextItem = object.get(i);
         titles.push( nextItem.get("title") );
    }
    return titles.join(",");

  • getIndex: Returns the index of the property's numeric position within the object. Returns zero if the property is not found.

    Usage:

    getIndex (string propertyName)

    Examples:

    ColdFusion: <cfset index = object.getIndex("title") />
    result: 2 // starting index of 1

    Javascript: index = object.getIndex("title");
    result:1 // starting index of 0

  • getLength: Returns the total number of properties of this object. This function is most useful when the PiComponent is being used as an Array, but it may also be used with named properties as well.

    Examples:

    ColdFusion:
    <cfloop from="1" to="#object.getLength()#" index="i">
         <cfset titleList = ListAppend(titleList, object.get(i).get("title")) />
    </cfloop>

    Javascript:
    for (var i=0, titles=[]; i < object.getLength(); i++)
         titles.push( object.get(i).get("title") );

  • getPrivate: [ColdFusion only] Returns a private scope of variables that are only available from within the object. Under the hood, it returns its own Pi object, so all of the same functions are available with the private scope.
  • Example:

    <cfset getPrivate().set("mySSN","111-11-1111") />
    <cfset addFilter("SSN",this,"maskSSN") />
    <cffunction name="maskSSN">
         <cfreturn "XXX-XX-" & Right(getPrivate().get("mySSN"),4) />
    </cffunction>

  • getProperty: Returns the name of the property at the given index. Returns the empty string if there is no property at the given index.

    Usage:

    getProperty (integer index)

    Examples:

    ColdFusion: <cfset prop = object.getProperty(1) />
    Javascript: prop = object.getProperty(0);
    result: "id"

  • getPropertyList: Returns a list of all of the properties of this object.

    Usage:

    1. getPropertyList ( ) : Returns a list of all the unique property names within the object.
    2. getPropertyList (string propertyName) : If the PiComponent is being used as an Array of other PiComponents, or as an array of Structs, you can optionally pass the name of a property to build a list of the values of that property for each item in the Array.

    Examples:

    ColdFusion: <cfset propertyList = object.getPropertyList() />
    Javascript: propertyList = object.getPropertyList();
    result: "id,title,template"

    ColdFusion: <cfset titleList = pages.getPropertyList("title") />
    result: "Creating Your Application.cfc,Creating a Site Template,Adding Breadcrumbs"
    Javascript: titles = pages.getPropertyList("title").split(",");
    result: ["Creating Your sitemap.xml", "Creating a Site Template", "Adding Breadcrumbs"]

  • getProtected: [ColdFusion only ] Returns a protected scope of variables that are only available to objects in the same folder. Under the hood, it returns its own Pi object, so all of the same functions are available with the protected scope.
  • Example:

    <!--- User data should not be available outside of this library. --->
    <cfset get("users").addListener(this,"protectUsers") />
    <cffunction name="protectUsers">
         <cfargument name="newValue" type="UserMunkey" />
         <cfset var lookup = Arguments.newValue.get("user_id") />
         <cfset getProtected().get("users").set(lookup, Arguments.newValue) />
         <!--- Store just the "user_id" in the public scope. --->
         <cfreturn Arguments.newValue.get("user_id") />
    </cffunction>

  • getUUID: Returns a Universally Unique Identifier (UUID). It's perfect to checking if two variables are equal to each other.

    Example:

    ColdFusion:
    <cfif object1.getUUID() eq object2.getUUID()>
         <!--- Same Object! --->
    </cfif>

    Javascript:
    if ( object1.getUUID() == object2.getUUID() )
         // Same Object!

  • toStruct: [ColdFusion only] A convenience function that translates the Pi object into a basic ColdFusion struct that can be dumped to a page for debugging purposes. The problem with dumping a Pi object through ColdFusion is that its internal data is hidden by design. With the toStruct function, you pass along a "depth" parameter to set how many levels to drill down when creating the structure. At each level, simple values will be added to the structure. Complex values will be expanded to show their members if they are within the depth requested. If they are beyond the depth requested, they will instead just show their type, e.g. "[Array]", "[Object]", or "[Struct]".
    • "depth": [default: 1] This parameter should be a number indicating how many level within the object should be added to the structure. For example, a depth of 1 will produce all of the properties of the object and any simple values, such as strings, numbers, dates, and booleans. Complex values will show their type, e.g. "[Array]", "[Object]", or "[Struct]", but no further details.

    Examples:

    PiObject.toStruct():

    struct
    ID Security
    PROPERTY1 Attribute 1
    PROPERTY2 Attribute 2
    property3 [Object]
    property4 [Array]

    PiObject.toStruct(2):

    struct
    ID Security
    PROPERTY1 Attribute 1
    PROPERTY2 Attribute 2
    property3
    struct
    ID SubObject
    PROPERTY1 Attribute 1
    PROPERTY2 [Struct]
    property4
    struct
    1 First
    2 Second
    3 Third
    4 Fourth
    5 Fifth


     

 

Follow Pete on Twitter

Copyright ©2010, MillionMunkeys® LLC, All Rights Reserved