CBWIRE
InstallSource CodeIssuesSupport
v3.x
v3.x
  • Introduction
  • Getting Started
  • Configuration
  • How It Works
  • Release History
    • What's New With 3.2
    • What's New With 3.1
    • What's New With 3.0
    • What's New With 2.2
    • What's New With 2.1
    • What's New With 2.0
  • Examples
    • Contact Form
    • Multi-select Input
    • File Upload
  • Resources
  • The Essentials
    • Components
    • Templates
    • Data Properties
    • Computed Properties
    • Actions
    • Events
    • Lifecycle Methods
    • JavaScript
    • Testing
  • Wire Features
    • Validation
    • File Uploads
    • Query String
    • Redirecting
    • WireBox
  • Template Features
    • Directives
    • Loading States
    • Polling
    • Prefetching
    • Offline State
    • Defer Loading
    • Dirty State
  • Integrations
    • ContentBox CMS
    • SPAs with Turbo
    • AlpineJS
Powered by GitBook
On this page
  • Defining Properties
  • Accessing Properties
  • Caching
  1. The Essentials

Computed Properties

Dynamic, cached, properties that can return any CFML data type.

PreviousData PropertiesNextActions

Last updated 1 year ago

Computed Properties are dynamic properties and are helpful for deriving values from a database or another persistent store like a cache.

Computed Properties are similar to with some key differences:

  • They are declared as inline functions using computed.

  • They can return any CFML data type, not just values that can be parsed by JavaScript like .

Defining Properties

You can define Computed Properties on your components using computed.

<cfscript>
    computed = {
        "tasks": function() {
            return queryExecute( "
                select *
                from tasks
            " );
        }
    };
</cfscript>

Accessing Properties

You can access Computed Properties in your component template using propertyName().

<cfscript>
    computed = {
        "currentTime": function() {
            return now();
        }
    };
</cfscript>

<cfoutput>
    <div>
        Current time: #currentTime()#
    </div>
</cfoutput>
<cfscript>
    computed = {
        "currentTime" : function() {
            return now();
        }
    };    
    
    function sendEmail() {
        if ( dateDiff( "d", computed.currentTime() ) > 3 ) {

        }
    }
</cfscript>

Computed Properties are defined as a CFML closure. That means that to get the result from the function, you must invoke it like so.

myComputedProperty() // good
myComputedProperty // bad

Caching

Computed Properties are cached for the lifetime of the request. If you reference your Computed Property three times in your component template or from within a component action, it will only execute once.

<cfscript>
    computed = {
        "getUUID": function() {
            return createUUID();
        }
    }
</cfscript>

<cfoutput>
    <div>
        <p>#getUUID()#</p>
        <p>#getUUID()#</p> <!-- Outputs the same ID because of caching -->
    </div>
</cfoutput>

You can prevent caching on a computed property by passing a false argument when invoking it.

<p>#getUUID()#</p>
<p>#getUUID( false )#</p> <!-- Does not use caching --->

You can also access Computed Properties from within your .

Data Properties
Data Properties
Actions