ComponentSpace acts as a registry for components and behaviors. It manages definitions, aliases, imports/exports, and global using configurations.
Creating a ComponentSpace
getDefaultComponentSpace(): Returns the global default component space instance.new ComponentSpace(defaultComponent?, baseSpace?, styleScopeManager?, allowUnusedNativeNode?): Creates a new space. You can pass an existing space as baseSpace to inherit its definitions.
Defining Components and Behaviors
You can define entities using several styles:
- Fluent/Chainable API: Use
space.define(is).property(...).data(...).method(...).registerComponent(). - Classic Style: Use
space.defineComponent(def) or space.defineBehavior(def) with a standard definition object. - Trait Behavior: Use
space.defineTraitBehavior(trans) to define behaviors with transformation functions. - Registration: Use
space.registerComponent(is, comp) to register an existing component definition.
Import, Export, and Global Using
- Exporting: Use
exportComponent(alias, is) or exportBehavior(alias, is) to make definitions available to other spaces. - Importing: Use
importSpace(protoDomain, space, privateUse) to bring in another space. Imported components are accessed via domain/name (e.g., space://lib/comp-name). - Global Using: Use
setGlobalUsingComponent(key, target) to make a component available to all components within that space. - Lazy Loading: Use
setComponentWaitingListener(listener) to handle scenarios where components are needed but not yet loaded (e.g., async loading).
const { ComponentSpace, getDefaultComponentSpace } = glassEasel
// Use default component space
const defaultSpace = getDefaultComponentSpace()
// Create new component space
const mySpace = new ComponentSpace('my-default-comp')
// Create space based on existing one (inheritance)
const childSpace = new ComponentSpace('default', defaultSpace)
// Update default options
mySpace.updateComponentOptions({
multipleSlots: true,
dataDeepCopy: glassEasel.DeepCopyKind.SimpleWithRecursion,
})
const space = new ComponentSpace()
// Chainable API definition
const MyComp = space
.define('my-comp')
.property('title', String)
.data(() => ({ count: 0 }))
.method('increment', function () {
this.setData({ count: this.data.count + 1 })
})
.registerComponent()
// Classic style definition
const MyComp2 = space.defineComponent({
is: 'my-comp-2',
properties: { title: String },
data: { count: 0 },
methods: {
increment() {
this.setData({ count: this.data.count + 1 })
},
},
})
// Exporting for other spaces
space.exportComponent('my-comp-alias', 'my-comp')
// Importing another space
space.importSpace('space://lib', libSpace, false)
// Global using
space.setGlobalUsingComponent('my-icon', iconCompDef)
// Placeholder listener for async loading
space.setComponentWaitingListener((isPub, alias, owner) => {
console.log(`Component "${alias}" is needed by`, owner.is)
})