DoneJS StealJS jQuery++ FuncUnit DocumentJS
6.0.1
5.33.2 4.3.0 3.14.1 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-bind
      • can-compute
      • can-debug
      • can-deep-observable
      • can-define
        • static
          • types
        • types
          • PropDefinition
          • ValueOptions
        • behaviors
          • default
          • Default
          • get
          • identity
          • serialize
          • set
          • type
          • Type
          • value
      • can-define/list/list
      • can-define/map/map
      • can-define-backup
      • can-define-stream
      • can-define-stream-kefir
      • can-event-queue
      • can-kefir
      • can-list
      • can-map
      • can-map-compat
      • can-map-define
      • can-observable-array
      • can-observable-object
      • can-observation
      • can-observation-recorder
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
      • can-value
    • Views
      • can-attribute-observable
      • can-component
      • can-observable-bindings
      • can-stache
      • can-stache-bindings
      • can-stache-converters
      • can-stache-element
      • can-stache-route-helpers
      • can-view-autorender
      • can-view-callbacks
      • can-view-import
      • can-view-model
      • can-view-parser
      • can-view-scope
      • can-view-target
      • steal-stache
    • Data Modeling
      • can-connect
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-tag
      • can-define-realtime-rest-model
      • can-define-rest-model
      • can-fixture
      • can-fixture-socket
      • can-local-store
      • can-memory-store
      • can-ndjson-stream
      • can-query-logic
      • can-realtime-rest-model
      • can-rest-model
      • can-set-legacy
      • can-super-model
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-hash
      • can-route-mock
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-diff
      • can-globals
      • can-join-uris
      • can-key
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-queues
      • can-string
      • can-string-to-any
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-child-nodes
      • can-control
      • can-dom-data
      • can-dom-events
      • can-dom-mutate
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-fragment
    • Data Validation
      • can-define-validate-validatejs
      • can-type
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-data-types
      • can-namespace
      • can-reflect
      • can-reflect-dependencies
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

can-define

  • npm package badge
  • Star
  • Edit on GitHub

Defines observable properties and their behavior on a prototype object. This function is not commonly used directly. can-define/map/map and can-define/list/list are more commonly used. Types and behaviors shared by both can-define/map/map and can-define/list/list are documented here.

define(prototype, propDefinitions)

The define function can be used to define observable properties, type conversion, and getter/setter logic on prototype objects. The define function is used by can-define/map/map and can-define/list/list to create observables. However, define can be used to create observables from types that do not inherit from can-define/map/map and can-define/list/list.

For more information on observables and how they are used in CanJS, please read the Technology Overview.

The following creates a Greeting type which will have observable message properties:

import {define, Reflect as canReflect} from "can";

const Greeting = function( message ) {
  this.message = message;
};

define( Greeting.prototype, {
  message: { type: "string" }
} );

const greeting = new Greeting("Hello");

canReflect.onKeyValue(greeting, "message", (newValue) => {
  console.log( newValue ); //-> logs "goodbye"
});

greeting.message = "goodbye";

Parameters

  1. prototype {Object}:

    The prototype object of a constructor function or class. The prototype object will have getter/setters defined on it that carry out the defined behavior. The prototype will also contain all of can-event-queue/map/map's methods.

  2. propDefinitions {Object<String,PropDefinition>}:

    An object of properties and their definitions. For example, a property (propertyName) has a PropDefinition object with zero or more of the following behaviors:

    define(Type.prototype, {
      propertyName: {
        default: function() { /* ... */ },
        Default: Constructor,
        type: function() { /* ... */ },
        Type: Constructor,
        get: function() { /* ... */ },
        value: function() { /* ... */ },
        set: function() { /* ... */ },
        serialize: function() { /* ... */ },
        identity: Boolean
      }
    })
    

Use

can-define provides a way to create custom types with observable properties. Where can-define/map/map and can-define/list/list provide more functionality, they also make more assumptions on the type constructor. can-define can be used to create completely customized types.

The following creates a Person constructor function that will be used to create Person instances with observable properties:

import {define} from "can";

// Define the type
const Person = function( first, last ) {
    this.first = first;
    this.last = last;
};
define( Person.prototype, {
    first: { type: "string" },
    last: { type: "string" },
    fullName: {
        get: function() {
            return this.first + " " + this.last;
        }
    }
} );

// Create an instance
const person = new Person( "Justin", "Meyer" );

console.log( person.first ); //-> "Justin"
console.log( person.last ); //-> "Meyer"
console.log( person.fullName ); //-> "Justin Meyer"

person.on( "fullName", function( ev, newVal, oldVal ) {
    console.log( newVal ); //-> "Ramiya Meyer"
    console.log( oldVal ); //-> "Justin Meyer"
} );

person.first = "Ramiya";

The observable properties call ObservationRecorder.add so they can be automatically by can-observation (and therefore can-stache).

Mixed-in instance methods and properties

define adds the following methods from can-event-queue/map/map:

  • addEventListener - Register an event handler to be called when an event is dispatched.

  • @can.getWhatIChange - Return observables whose values are affected by attached event handlers

  • @can.isBound - Return if the observable is bound to.

  • @can.offKeyValue - Unregister an event handler to be called when an event is dispatched.

  • @can.onKeyValue - Register an event handler to be called when a key value changes.

  • dispatch - Dispatch event and key binding handlers.

  • listenTo - Listen to an event and register the binding for simplified unbinding.

  • off - A shorthand method for unbinding an event.

  • on - A shorthand method for listening to event.

  • one - Register an event handler that gets called only once.

  • removeEventListener - Unregister an event handler to be called when an event is dispatched.

  • stopListening - Stops listening for registered event handlers.

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 6.0.1.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news