Blueprint
  • Blueprint Developer Guide
  • Quick Start
    • Getting Started
    • My First Application
      • Creating Your Application
      • Controllers
      • Routers & Routes
      • Services
      • Resources & Resource Controllers
      • Validating & Sanitizing Input
      • Unit Testing Your Application
      • Policies
  • Developer Guide
    • The Object Model
      • Introduction
      • Classes and Instances
      • Computed Properties
      • Aggregated Properties
      • Mixins
    • Routers and Controllers
      • Introduction
      • Routers
      • Controllers
      • Resources
    • Models
    • The Server
    • Policy Framework
    • Services
    • Messaging Framework
    • Configuration Management
    • Application and Resources
      • Lookup Operation
      • Views
      • Assets
    • Blueprint Modules
    • Blueprint Cluster
      • What is a Blueprint Cluster?
      • Running a Blueprint Cluster
      • Technical Details
    • Testing Framework
    • Command-line Interface (Coming Soon)
Powered by GitBook
On this page
  • Overview
  • Creating a Mixin
  • Using a Mixin
  • Applying Mixin to Class Definition
  • Applying Mixin to Object Instance
  1. Developer Guide
  2. The Object Model

Mixins

Learn how to create and apply mixins

Overview

A mixin is an entity that captures reusable data properties and methods, but cannot be instantiated. The reason we have mixins is because the our object model uses single inheritance—meaning a class can only have one class as its base class. If we want multiple classes to share data properties and methods from multiple entity types, then it is not possible.

Creating a Mixin

You create a mixin using the Mixin.create() method. The create method takes a definition hash that contains data properties and methods, which is similar to how you created a class.

events.js
const {Mixin} = require ('@onehilltech/blueprint');

const Events = Mixin.create ({
  on (name, method) { 
    // ...
  },
  once (name, method) { 
    // ...
  }
  emit (name, ...args) { 
    // ...
  }
});

Using a Mixin

Applying Mixin to Class Definition

The extend() method takes optional list of mixins before the class definition.

The signature of the extend() method isBO.extend ([Mixin1, Mixin2, Mixin3,] definition).

For example, let's assume we want to mix in the Events mixin with the Person class. We can do that by preceding the definition with the mixin.

const { BO } = require ('@onehilltech/blueprint');
const Events = require ('./events');

const Person = BO.extend (Events, {
  // The person class definition
});

Now, anytime we create a Person class, it will have access to the methods and data properties defined in the Events mixin.

Applying Mixin to Object Instance

Similar to the extend() method, the create() method takes optional list of mixins that before the class definition.

The signature of the create() method isBO.create ([Mixin1, Mixin2, Mixin3,] definition)

For example, let's assume we want to mix in the Events mixin with the Person class. We can do that by preceding the definition with the mixin.

const { BO } = require ('@onehilltech/blueprint');
const Events = require ('./events');

let president = Person.create (Events, {
  firstName: 'Barack',
  lastName: 'Obama'
});

Now, only the president instance will have access to the methods and data properties defined in the Events mixin.

PreviousAggregated PropertiesNextRouters and Controllers

Last updated 7 years ago

Once we have created a mixin, you can use it in two ways. The first approach is . When you apply a mixin to a class definition, all instances of the class will have the mixin as part of their definition. The second approach is to when it is created. When we use this approach, only the created instance will have the mixin as part of its definition. It does not impact the other instances of the class.

applying the mixin to the class definition
apply the mixin to an instance