CBWIRE
v4.x
v4.x
  • Introduction
  • How It Works
  • Getting Started
  • Configuration
  • Releases
    • What's New With 4.1
    • What's New With 4.0
    • 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
  • Resources
  • Upgrading from CBWIRE 3.x
  • Upgrading from CBWIRE 2.x
  • CBWIRE CLI
  • The Essentials
    • Components
    • Templates
    • Data Properties
    • Computed Properties
    • Data Binding
    • Actions
    • Events
    • Nesting Components
    • Lifecycle Methods
    • JavaScript
    • Testing
  • Features
    • Single-file Components
    • Alpine.js
    • Lazy Loading
    • Form Validation
    • File Uploads
    • Query String
    • Redirecting
    • WireBox
  • Template Directives
    • wire:click
    • wire:confirm
    • wire:dirty
    • wire:ignore
    • wire:init
    • wire:key
    • wire:loading
    • wire:model
    • wire:navigate
    • wire:offline
    • wire:poll
    • wire:stream
    • wire:submit
    • wire:transition
  • Advanced
    • Troubleshooting
Powered by GitBook
On this page
  • Overview
  • Single File Format
  • Performance

Was this helpful?

Export as PDF
  1. Features

Single-file Components

PreviousTestingNextAlpine.js

Last updated 7 months ago

Was this helpful?

Overview

are typically built with a .cfc file and a .cfm file. You can combine these into a single .cfm file, similar to how components in Vue.js are built.

Let's look at the example from the that uses two separate files.

// ./wires/Counter.cfc
component extends="cbwire.models.Component" {   
    data = {
        "counter": 0
    };
    function onMount() {
        // Load the counter from the session
        data.counter = session.counter ?: 0;
    }
    function save( counter ) {
        // Save the counter to the session
        session.counter = arguments.counter;
    }
}
<!--- ./wires/counter.cfm --->
<cfoutput>
    <div
        x-data="{
            counter: $wire.counter,
            increment() {
                this.counter++   
            },
            async save() {
                // Call the save method on our component
                await $wire.save( this.counter );
            }
        }"
        wire:ignore.self>
        <div>Count: <span x-text="counter"></span></div>
        <button @click="increment">+</button>
        <button @click="save">Save</button>
    </div>
</cfoutput>

Single File Format

Let's combine these into a single file.

<!--- ./wires/counter.cfm --->
<cfoutput>
    <div
        x-data="{
            counter: $wire.counter,
            increment() {
                this.counter++   
            },
            async save() {
                // Call the save method on our component
                await $wire.save( this.counter );
            }
        }"
        wire:ignore.self>
        <div>Count: <span x-text="counter"></span></div>
        <button @click="increment">+</button>
        <button @click="save">Save</button>
    </div>
</cfoutput>

<cfscript>
    // @startWire
    data = {
        "counter": 0
    };
    function onMount() {
        // Load the counter from the session
        data.counter = session.counter ?: 0;
    }
    function save( counter ) {
        // Save the counter to the session
        session.counter = arguments.counter;
    }
    // @endWire
</cfscript>

There are only a few differences with the single-file format.

  • The contents of your .cfc file are instead placed inside a <cfscript> block

  • The // @startWire and // @endWire comment markers have been added so that CBWIRE can parse the file.

You must add the //@startWire and //@endWire markers when using single-file format.

You can place your <cfscript> block above or below your template. CBWIRE parses this out before processing.

Performance

There is a slight performance hit when using the single file format because CBWIRE has to parse out the sections of your file. However, this hit is minimal because of the internal caching CBWIRE uses.

Cached files can be found under ./modules/cbwire/models/tmp. This directory gets cleared anytime you reinit your application using ColdBox's fwreinit.

http://myapp/?fwreinit=true

Components
introduction